2016-04-19 6 views
1

Ich habe Kontrollkästchen Code in einer CardView-Layout-Datei. Die CardView hat einen weißen Hintergrund. Normalerweise denke ich, dass die ungeprüfte Checkbox ein schwarzes Quadrat ist. Mein Layout zeigt kein leeres Kontrollkästchen. Alles, was ich sehe, ist nur der weiße CardView-Hintergrund (oben CardView im Screenshot). Wenn ich auf den am weitesten rechts liegenden Bereich der CardView klicke, wo der Checkbox-Code formatiert ist, erscheint eine grüne Checkbox (untere CardView im Screenshot). Was fehlt mir hier? enter image description here.Android: Warum zeigt Checkbox kein leeres Kontrollkästchen für den deaktivierten Status an?

Layout-Datei:

<LinearLayout 

xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@color/background4main" > 

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/singlecard_view1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardBackgroundColor="@android:color/white" 
    card_view:cardCornerRadius="6dp" 
    android:orientation="horizontal" 
    android:layout_margin="4dp"> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?android:selectableItemBackground" > 

    <TextView 
     android:id="@+id/cardBlankText2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:text="todo" 
     android:textStyle="bold" 
     android:textColor="@android:color/black" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/cardBlankText3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/cardBlankText2" 
     android:text="note1" 
     android:textStyle="bold" 
     android:textColor="@android:color/black" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20sp" /> 

    <CheckBox 
     android:id="@+id/chkSelected" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_centerVertical="true" /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

</LinearLayout> 

Adapter file: 
... 
public class ListViewHolder extends RecyclerView.ViewHolder { 

    TextView cardBlankText2; 
    TextView cardBlankText3; 
    CheckBox chkSelected; 

    public ListViewHolder(View itemView) { 
     super(itemView); 

     cardBlankText2 = (TextView)itemView.findViewById(R.id.cardBlankText2); 
     cardBlankText3 = (TextView)itemView.findViewById(R.id.cardBlankText3); 
     chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected); 
    } 
... 
@Override 
public void onBindViewHolder(final ListViewHolder holder, final int position) { 

     holder.cardBlankText2.setText(dbList.get(position).getTodo()); 
     holder.cardBlankText3.setText(dbList.get(position).getNote1()); 
     holder.chkSelected.setChecked(dbList.get(position).isSelected()); 
     holder.chkSelected.setTag(dbList.get(position)); 
} 
+0

Post Java-Code –

+0

ok wird nun hinzufügen. – AJW

+0

Was passiert, wenn Sie das gleiche Kontrollkästchen deaktivieren? Wird es wieder wie der erste Screenshot angezeigt? –

Antwort

1

Mögliche Gründe:

  • Ihre Checkbox in weißer Farbe sein könnte.

  • Es ist VISIBILITY

Fügen Sie auch toLeftOf Attribut für die Textview ist verschwunden sein, so dass sie mit dem checkbox.Check Ihr Thema für die Farbe der checkbox.If nicht überlappen können, die Sie nicht können nicht helfen setzen ziehbar ?android:attr/listChoiceIndicatorMultiple für unkontrollierten Zustand

+0

ok werde ich versuchen. Wie überprüfe ich, welche Farbe das Kontrollkästchen für mein Theme hat? – AJW

+0

siehe http://StackOverflow.com/A/26850668/4321808 –

+0

@RT "TextColorSecondary" in meinem Thema hat den Trick. Antwort abgestimmt und angenommen. Prost. – AJW

2

drawable customcheckbox.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="false" android:drawable="@drawable/unchecked_drawable" /> 
    <item android:state_checked="true" android:drawable="@drawable/checked_drawable" /> 
    <item android:drawable="@drawable/unchecked_drawable" /> <!-- default state --> 
    </selector> 

und Ihre xML-Datei:

<CheckBox 
android:id="@+id/check" 
android:button="@drawable/customdrawablecheckbox" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
2

Verwenden Sie den folgenden Code:

<android.support.v7.widget.AppCompatCheckBox 
       android:id="@+id/settings_notification_checkbox" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:checked="true" 
       app:buttonTint="@color/colorPrimary"/> 

Statt dessen:

<CheckBox 
     android:id="@+id/check" 
     android:button="@drawable/customdrawablecheckbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
Verwandte Themen