复选框外观不一致
Inconsistent checkbox appearance
我正在制作具有以下属性的应用程序:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
目前我使用了 3 种不同的方法来显示复选框,而且它们的显示方式都不一样!
1) 从 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>
代码:
// 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();
结果是一个看起来很不错的复选框,因为它符合对话框的样式:
2) 在代码中创建:
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();
结果是一个复选框,与添加到的对话框相比,它看起来很暗且不合时宜:
有趣的是,在我开始支持更高版本的 Android 之前,1 和 2 看起来是一样的。
3) XML 和 Activity.setContentView:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
activity Studio 中的 Android 预览显示了预期的结果:
此复选框使用另一种样式,但在本例中它适合 Activity.
的深色样式
但是,当我在我的 Moto G 上测试应用程序时,复选框没有出现:
任何人都可以阐明这一点吗?为什么 Checkboxes 会以这 3 种不同的样式出现,我该如何控制它们?为什么最后一个 Checkbox 根本没有出现?
编辑: 最后一个问题似乎是由我的 Activity 使用继承自 Theme.AppCompat:
的样式引起的
<style name="Menu" parent="@style/Theme.AppCompat" >
如果我删除父主题,复选框会重新出现。因此,我的 appcompat 设置可能存在一些问题。这是我在 build.gradle:
中添加依赖项的方式
dependencies {
compile 'com.android.support:appcompat-v7:22.1.1'
}
问题是由于更改v7支持库的版本引起的。
如果我的 build.gradle
包含以下行:
compile 'com.android.support:appcompat-v7:20.0.0'
然后所有复选框看起来都一样,但是一旦我将其升级到,说:
compile 'com.android.support:appcompat-v7:21.0.0'
复选框的外观发生变化。
Checkbox消失的情况,是因为Checkbox是全黑的,所以和背景融为一体。
我正在制作具有以下属性的应用程序:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
目前我使用了 3 种不同的方法来显示复选框,而且它们的显示方式都不一样!
1) 从 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>
代码:
// 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();
结果是一个看起来很不错的复选框,因为它符合对话框的样式:
2) 在代码中创建:
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();
结果是一个复选框,与添加到的对话框相比,它看起来很暗且不合时宜:
有趣的是,在我开始支持更高版本的 Android 之前,1 和 2 看起来是一样的。
3) XML 和 Activity.setContentView:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
activity Studio 中的 Android 预览显示了预期的结果:
此复选框使用另一种样式,但在本例中它适合 Activity.
的深色样式但是,当我在我的 Moto G 上测试应用程序时,复选框没有出现:
任何人都可以阐明这一点吗?为什么 Checkboxes 会以这 3 种不同的样式出现,我该如何控制它们?为什么最后一个 Checkbox 根本没有出现?
编辑: 最后一个问题似乎是由我的 Activity 使用继承自 Theme.AppCompat:
的样式引起的<style name="Menu" parent="@style/Theme.AppCompat" >
如果我删除父主题,复选框会重新出现。因此,我的 appcompat 设置可能存在一些问题。这是我在 build.gradle:
中添加依赖项的方式dependencies {
compile 'com.android.support:appcompat-v7:22.1.1'
}
问题是由于更改v7支持库的版本引起的。
如果我的 build.gradle
包含以下行:
compile 'com.android.support:appcompat-v7:20.0.0'
然后所有复选框看起来都一样,但是一旦我将其升级到,说:
compile 'com.android.support:appcompat-v7:21.0.0'
复选框的外观发生变化。
Checkbox消失的情况,是因为Checkbox是全黑的,所以和背景融为一体。