【问题标题】:Android AlertDialog title background colorAndroid AlertDialog 标题背景颜色
【发布时间】:2015-12-12 12:50:17
【问题描述】:

有没有办法改变AlertDialog(android.support.v7.app.AlertDialog)的标题背景颜色?? 目前在我的主题中我有

 <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>

 <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
  </style>

我是这样理解的,

我怎样才能让它看起来像这样,

使用

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowTitleStyle">@style/DialogTitle</item>
    </style>

    <style name="DialogTitle">
        <item name="android:background">@color/colorPrimary</item>
    </style>

给予

关于如何实现这一点的任何想法?

【问题讨论】:

  • 你找到解决办法了吗?
  • 我觉得松国先生的回答是最好的回答stackoverflow.com/a/42135263/7797592
  • 看起来setCustomTitle() 是唯一的方法。
  • @Hunt 我在下面发布了我的答案。

标签: android android-alertdialog android-theme


【解决方案1】:

你可以像这样设置自定义标题

LayoutInflater inflater = this.getLayoutInflater();

    View titleView = inflater.inflate(R.layout.custom_title, null);

    new AlertDialog.Builder(SubCategoryActivity.this)
                        .setCustomTitle(titleView);

在 custom_title 布局中,您可以像这样创建自定义标题

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:id="@+id/llsubhead"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/exemptionSubHeading4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:text="Exemption Sub Head"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/white" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 这可以设置背景颜色,但我似乎无法让AlertDialog.Builder.setTitle("Example Customized Title") 工作。如果我在android:text 字段中保留默认值,则会显示该值,否则标题文本为空白。
  • 解决这个问题的方法很简单;只需使用titleView.findViewById(R.id.exemptionSubHeading4)(或您提供给TextView 的任何ID),然后使用setText("Example Customized Title")。增加约 1 个额外步骤,但为您提供完全自定义。
  • 是的,您需要这样做。
【解决方案2】:

我猜,自定义样式不起作用的原因是因为默认填充(围绕整个 AlertDialog 布局)仍然存在:

<style name="myAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowTitleStyle">@style/myDialogTitle</item>
</style>

<style name="myDialogTitle" parent="TextAppearance.AppCompat.Title">
    <item name="android:background">@color/colorPrimary</item>
</style>

getResource.getIdentifier()findViewByid() 也不起作用(到目前为止我已经尝试过了)。因此,setCustomTitle() 成为唯一的办法。


1.为 AlertDialog 创建自定义标题布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light"
    android:orientation="vertical"
    android:paddingStart="24dp"                         //following Material Design guideline
    android:paddingTop="16dp"
    android:paddingEnd="24dp"
    android:paddingBottom="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Marvel Cinematic Universe"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"      //key point 1, using AlertDialog default text style
        android:textColor="@android:color/white" />

</LinearLayout>

2。使用“setCustomTitle()”将自定义标题布局应用到 AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
View titleView = getLayoutInflater().inflate(R.layout.dialog_title, null);

builder.setCustomTitle(titleView);                      //key point 2
builder.setMessage(R.string.main_marvel_info);
builder.setPositiveButton("OK", null);

builder.create().show();

【讨论】:

    【解决方案3】:

    使用自定义 Alerbox,点击时使用此代码。我制作了自定义布局“alert_input”,将显示“确定”和“取消”选项

     LayoutInflater layoutInflater = LayoutInflater.from(Login.this);
                    View promptView = layoutInflater.inflate(R.layout.alert_input, null);
                    final EditText editText = (EditText) promptView.findViewById(R.id.alertEdit2);
                    final EditText editText2 = (EditText) promptView.findViewById(R.id.alertEdit3);
                    final TextView at=(TextView)findViewById(R.id.alertText);
    
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this,AlertDialog.THEME_HOLO_LIGHT);
                    alertDialogBuilder.setView(promptView);
                    alertDialogBuilder.setCancelable(false)
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
    
    
                                }
                            })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    });
    
                    // create an alert dialog
                    AlertDialog alert = alertDialogBuilder.create();
                    alert.show();
    

    【讨论】:

      【解决方案4】:

      androidx.appcompat.app.AlertDialog

      AlertDialog alertdialog;
      ...
      alertdialog.show();
      final View v = alertdialog.findViewById(R.id.title_template);
      if (v != null)
          v.setBackgroundColor(myColor);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多