【问题标题】:AlertDialog text color and marginAlertDialog 文本颜色和边距
【发布时间】:2015-02-18 09:25:27
【问题描述】:

我的目标是 API 级别 8+。我需要一个带有标题、消息、带有一些文本的复选框和两个经典底部按钮的对话框。 我正在尝试使用标准的 AlertDialog 和方法 setView 来添加复选框:

checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_checkbox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp" >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:checked="true"
        android:textSize="14sp" >
    </CheckBox>

</FrameLayout>

showDialog 方法

private void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);

    builder.setTitle("Title");
    builder.setMessage("Message");

    View checkboxView = View.inflate(activity, R.layout.checkbox, null);
    CheckBox checkBox = (CheckBox) checkboxView.findViewById(R.id.checkbox);
    checkBox.setText("Some checkbox text");
    builder.setView(checkboxView);

    // First button
    builder.setNegativeButton(getString(R.string.not_now),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

            });

    // Second button
    builder.setPositiveButton(getString(R.string.help),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
            });

    builder.create();
    builder.show();
}

我的 AppBaseTheme 有父级“Theme.AppCompat.Light”(/res/values/styles.xml),对于 values-v11 和“Theme.AppCompat.Light.DarkActionBar”对于 values-v14 相同。 我在 2.3.3 中遇到问题,复选框和文本没有边距或填充(我已在 xml 中设置它们),除了文本是黑色作为背景。在 4.4 中一切看起来都很好。 我已经阅读了很多关于这个问题的东西,但我仍然感到困惑并且没有解决方案。你能帮帮我吗?

【问题讨论】:

标签: android android-layout android-dialog android-dialogfragment android-styles


【解决方案1】:

您可以使用以下命令强制对话框为白色背景:

if(Build.VERSION.SDK_INT <= 10) {
    builder.setInverseBackgroundForced(true);
}

【讨论】:

  • 这样我只有我的自定义复选框视图,白色背景,没有边距,整个对话框仍然是黑色的。无论如何,我还有很多其他黑色背景的对话框,所以我不能为此做一个例外。我真的不能将相同的基本样式应用于自定义视图吗?
猜你喜欢
  • 2020-09-01
  • 2019-04-11
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
相关资源
最近更新 更多