mardi 5 mai 2015

Inconsistent checkbox appearance

I am making an app with the following properties:

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="22" />

Currently I am using 3 different methods to display Checkboxes, and all of them appear differently!

1) Inflated from XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <CheckBox 
        android:id="@+id/dialog_fish_autoeat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/dialog_shop_autoeat"/>

</LinearLayout>

Code:

// Passing null as the parent, as this is the layout for an AlertDialog
final View inflate = game.getLayoutInflater().inflate(
            R.layout.dialog_fish, null);
builder.setView(inflate);
builder.show();

The result is a Checkbox that looks quite good as it fits with the style of the dialog:

Checkbox 1 - looks good

2) Created in code:

final CheckBox autoEquip;
autoEquip = new CheckBox(game);
autoEquip.setText(R.string.dialog_shop_autoequip);
autoEquip.setChecked(newStrength > oldStrength);
AlertDialog.Builder builder = new AlertDialog.Builder(game);
builder.setView(autoEquip);
builder.show();

The result is a checkbox that looks dark and out-of-place compared to the dialog it's added to:

Checkbox 2 - too dark

Interestingly, 1 and 2 used to look the same before I started supporting later versions of Android.

3) XML and Activity.setContentView:

<LinearLayout
    xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:gravity="center_vertical|center_horizontal"
    android:background="@color/menu_bg"
    tools:context=".Menu" >

    <CheckBox
        android:id="@+id/endgame_submit_score"
        android:text="@string/endgame_submit_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true" />

</LinearLayout>

The activity preview in Android Studio shows the expected result:

Checkbox 3 preview - looks good

This Checkbox uses yet another style, but in this case it fits with the dark style of the Activity.

However, when I test the app on my Moto G, the checkbox doesn't appear:

Checkbox 4 - not there

Can anyone shed some light on this? Why do the Checkboxes appear in these 3 different styles, and how can I control them? Why does the last Checkbox not appear at all?




Aucun commentaire:

Enregistrer un commentaire