【问题标题】:Styling titleDivider in Dialog对话框中的样式titleDivider
【发布时间】:2012-06-14 06:35:37
【问题描述】:

我想知道如何在对话框中摆脱(或更改颜色)titleDivider。它是蜂窝+设备上显示的对话框标题下方的蓝线。

我猜这是来自 SDK 的相关布局,但由于没有样式属性,我不知道如何设置它的样式。如果我尝试使用 findViewById 则没有 android.R.id.titleDivider

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <TextView android:id="@android:id/title" style="?android:attr/windowTitleStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="@android:dimen/alert_dialog_title_height"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:gravity="center_vertical|left" />
    <View android:id="@+id/titleDivider"
            android:layout_width="match_parent"
            android:layout_height="2dip"
            android:background="@android:color/holo_blue_light" />
    <FrameLayout
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:foreground="?android:attr/windowContentOverlay">
        <FrameLayout android:id="@android:id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
</LinearLayout>

我试图覆盖在我的 theme.xml 中仅引用 dialog_title_holo.xml 的 dialogTitleDecorLayout,但没有成功。错误是:

错误:错误:找不到与给定名称匹配的资源:attr 'dialogTitleDecorLayout'。

【问题讨论】:

  • 如果你想摆脱默认样式的某些部分,你必须完全删除它,因为它因设备和操作系统版本而异
  • 张贴你的截图@Sharad Mhaske
  • @GrlsHu:截图我发了
  • 看我的编辑回答,它会帮助你
  • 您始终可以使用自定义布局。设计您的自定义布局并将其放入 inn dailog

标签: android dialog divider


【解决方案1】:

获取对AlertDialogtitleDivider 的引用以更改其颜色:

int divierId = dialog.getContext().getResources()
                .getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(divierId);
divider.setBackgroundColor(getResources().getColor(R.color.creamcolor));

【讨论】:

  • 很好的答案,非常感谢。对于空指针的东西,您需要检查 frameworks/base/core/res/res/layout/alert_dialog.xml 文件或仅在 HierarchyViewer 中,确切的资源名称是什么
  • 分隔符为空。与 dialog.findViewById(android.R.id.message) 相同的自定义消息。这是为什么呢?
  • 请务必检查亚马逊kindle设备,因为它默认不显示蓝线。
  • @test 我也使用此代码 sn-p 得到了一个空指针异常,但仅限于 CyanogenMod roms。此代码在 MIUI rom 上完美运行。有没有一种全球性的方法可以做到这一点??
  • 如果使用DialogFragment,则此示例中的dialoggetDialog(),而不是getView()。并始终检查if (divider != null)
【解决方案2】:

你需要实现

myDialog = builder.create();
myDialog.setOnShowListener(new OnShowListenerMultiple());

//----------------------------
//Function to change the color of title and divider of AlertDialog
public static class OnShowListenerMultiple implements DialogInterface.OnShowListener {
    @Override
    public void onShow( DialogInterface dialog ) {
        if( !(dialog instanceof Dialog) )
            return;

        Dialog d = ((Dialog) dialog);
        final Resources resources = d.getContext().getResources();
        final int color = AppUtility.getColor( resources, R.color.defaultColor );

        try {
            int titleId = resources.getIdentifier( "android:id/alertTitle", null, null );
            TextView titleView = d.findViewById( titleId );
            titleView.setTextColor( color );
        }
        catch( Exception e ) {
            Log.e( "XXXXXX", "alertTitle could not change color" );
        }

        try {
            int divierId = resources.getIdentifier( "android:id/titleDivider", null, null );
            View divider = d.findViewById( divierId );
            divider.setBackgroundColor( color );
        }
        catch( Exception e ) {
            Log.e( "XXXXXX", "titleDivider could not change color" );
        }
    }
}

【讨论】:

  • 你能说出按钮分隔符的标识符吗?
【解决方案3】:

我通过使用 DialogFragment.STYLE_NO_TITLE 主题解决了这个问题,然后在对话框布局中伪造标题栏。

【讨论】:

  • 好主意。在找到您的答案之前,我一直在努力解决完全相同的问题。我担心我必须复制警报对话框布局并根据我的需要进行调整,但您的解决方案要好得多。 Android 有时会让如此简单的任务变得如此困难,这有点愚蠢......
  • @Andraz:我知道你之前做过这项工作,但希望你能分享更多细节。例如,您是如何设置主题的?这是否具有应用程序范围的效果,或者您可以将其限制在对话框中吗?另外,你是如何“伪造”标题栏的?
  • setStyle(SherlockDialogFragment.STYLE_NO_TITLE, R.style.Your_Style) - 我使用的是 SherlockActionBar。然后我用图像伪造它(实际上我希望分隔栏与标题栏颜色相同 - 橙色)。不太记得所有细节,很久以前。
【解决方案4】:

这是我解决这个问题的方法(感谢http://joerg-richter.fuyosoft.com/?p=181):

MyDialogBu​​ilder.class

public class MyDialogBuilder extends android.app.AlertDialog.Builder {

public MyDialogBuilder(Context context) {
    super(context);
}

@NonNull
@Override
public android.app.AlertDialog create() {
    final android.app.AlertDialog alertDialog = super.create();

    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            int titleDividerId = getContext().getResources()
                    .getIdentifier("titleDivider", "id", "android");

            View titleDivider = alertDialog.findViewById(titleDividerId);
            if (titleDivider != null) {
                titleDivider.setBackgroundColor(getContext().getResources()
                        .getColor(R.color.alert_dialog_divider));
            }
        }
    });

    return alertDialog;
}
}

【讨论】:

    【解决方案5】:

    使用

     <View android:id="@+id/titleDivider"
            android:layout_width="match_parent"
            android:layout_height="2dip"
            android:background=#CC3232 />
    

    【讨论】:

    • 我发布的xml是android源码的一部分,所以我不能真正改变它
    【解决方案6】:

    在写dialog.show()之前,写:

    int divierId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider",   null, null);
    View divider = dialog.findViewById(divierId);
    if(divider!=null){
    divider.setBackgroundColor(getResources().getColor(R.color.transparent));}
    

    colors.xml 中:

    <color name="transparent">#00000000</color>
    

    【讨论】:

    • 这对我有用,但你应该对分隔符进行空指针检查。这不适用于 5.0 及更高版本。
    【解决方案7】:

    如果您不想使用默认样式,请不要使用 AlertDialog。您可以使用带有对话框主题的活动(使用您的自定义布局)。

    <activity android:theme="@android:style/Theme.Dialog">
    

    【讨论】:

      【解决方案8】:

      这个是在一些 4.x 设备上测试的:

          TextView title = (TextView)getWindow().getDecorView().findViewById(android.R.id.title);
          ((ViewGroup)title.getParent()).getChildAt(1).setVisibility(View.GONE);
      

      【讨论】:

      • 这产生了一个 NPE。根据原始问题消除标题分隔符的目的是什么?还是别的什么?
      【解决方案9】:

      你的想法是正确的。但是,您要查找的 dialogTitleDecorLayout 是私有资源,因此您无法以正常方式访问它。但是你仍然可以使用 * 语法访问它:

      <item name="*android:dialogTitleDecorLayout">@layout/dialog_title</item>
      

      将其添加到我自己的样式中,只需将 dialog_title.xml 复制到我的应用程序并稍作更改即可解决我的问题。

      【讨论】:

        【解决方案10】:

        你看this吗,有一个pcecial库,你可以看there。最后一个link会解决你的问题

        【讨论】:

          【解决方案11】:

          您可以像这样制作自定义对话框:

              Dialog dialog = new Dialog(this);
              dialog.setContentView(R.layout.custom_dialog);
              Button okay = (Button) dialog.findViewById(R.id.button1);
              okay.setOnClickListener(new OnClickListener() {
          
                   public void onClick(View arg0) {
          
                     // do your work
                   }
              });
          

          在布局中设置自定义标题不要使用android

               dialog.setTitle();
          

          还有你的 custom_dialog.xml

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:android1="http://schemas.android.com/apk/res/android"
             android:id="@+id/layout_root"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:orientation="vertical"
             android:padding="10dp">
          
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:textSize="40sp" 
                android:text="Hello"/>
          
          
              <Button
                  android:id="@+id/button1"
                  android:layout_width="100dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="150dp"
                  android:text="OK" />
          
              </RelativeLayout>
          

          【讨论】:

            【解决方案12】:

            如果我猜对了,“删除蓝线”意味着删除对话框标题和正文之间的边框。该边框来自 Holo 主题,因此在不使用自定义布局的情况下无法删除它。

            使用以下内容创建一个名为 custom-dialog.xml 的文件(这只是一个示例..根据需要修改它):

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/general_dialog_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
            
                <ImageView
                    android:id="@+id/dialogTopImage"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.12"
                    android:padding="10dp" />
            
                <LinearLayout
                    android:id="@+id/dialogLine"
                    android:layout_width="fill_parent"
                    android:layout_height="3dp"
                    android:background="@drawable/green_btn"
                    android:orientation="vertical" />
            
                <TextView
                    android:id="@+id/dialogText"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.32"
                    android:padding="5dp"
                    android:text=""
                     />
            
                <LinearLayout
                    android:id="@+id/general_dialog_layout"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_gravity="center"
                    android:layout_marginBottom="5dp"
                    android:layout_weight="0.11"
                    android:gravity="center"
                    android:orientation="horizontal" >
            
                    <Button
                        android:id="@+id/dialogButton"
                        android:layout_width="100dp"
                        android:textSize="8pt"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:background="@drawable/green_btn"
                        android:gravity="center"
                        android:text="Ok" />
            
            </LinearLayout>
            

            如您所见,我正在使用您的项目中不存在的资源和东西,但您可以安全地删除它们。在我的情况下,结果或多或少如下,顶部有一张图片,我将以编程方式在代码中设置。

            要创建对话框,请使用以下内容:

            private Dialog createAndShowCustomDialog(String message, Boolean positive, Drawable d, View.OnClickListener cl, String text1) {
            
                final Dialog dialog = new Dialog(this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.general_dialog_layout);
                // BIND
                ImageView image = (ImageView) dialog.findViewById(R.id.dialogTopImage);
                TextView text = (TextView) dialog.findViewById(R.id.dialogText);
                Button button = (Button) dialog.findViewById(R.id.dialogButton);
                LinearLayout line = (LinearLayout) dialog.findViewById(R.id.dialogLine);
            
                // SET WIDTH AND HEIGHT
                DisplayMetrics displaymetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                int width = (int) (displaymetrics.widthPixels * 0.85);
                int height = (int) (displaymetrics.heightPixels * 0.60);
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.width = width;
                dialog.getWindow().setLayout(width, height);
            
            
                // SET TEXTS
                text.setText(message);
                button.setText(text1);
            
                // SET IMAGE
                if (d == null) {
                    image.setImageDrawable(getResources().getDrawable(R.drawable.font_error_red));
                } else {
                    image.setImageDrawable(d);
                }
            
                // SET ACTION
                if (cl == null) {
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                } else {
                    button.setOnClickListener(cl);
                }
            
            
                // SHOW
                dialog.show();
                return dialog;
            }
            

            【讨论】:

              【解决方案13】:

              这些是无法通过控制兄弟隐藏的。我遇到了同样的问题。您唯一能做的就是创建自己的自定义对话框

              Here is a sample App

              下载看看设计模式,就很简单了

              Here is one Tutorial关于制作自定义对话框

              重要的部分是在创建 DialogObject 之后不要通过 setTitle() 设置标题 在您的 CustomLayout 中创建 TextView 并从 findViewByID() 调用它并设置您的标题

              【讨论】:

                【解决方案14】:

                在colors.xml中:

                <color name="transparent">#00000000</color>
                

                在对话框中:

                int divierId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider",null, null);

                查看分隔符 = d.findViewById(divierId); divider.setBackgroundColor(getResources().getColor(R.color.transparent));

                【讨论】:

                  【解决方案15】:

                  为了完全隐藏默认的蓝线(假设你在DialogFragment):

                      Dialog dialog = getDialog();
                      if (dialog != null) {
                          final int dividerId = dialog.getContext().getResources()
                                  .getIdentifier("android:id/titleDivider", null, null);
                          View divider = dialog.findViewById(dividerId);
                          if  (divider != null) {
                              divider.setBackground(null);
                          }
                      }
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2016-12-28
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多