Styling titleDivider in Dialog

2019-01-14 21:27发布

I am wondering how it is possible to get rid of (or change color) titleDivider in Dialog. It is a blue line below dialog title shown on honeycomb+ devices.

Annoying titleDivider line

I guess this is relevant piece of layout from SDK, but since there is no style attribute I dont know how to style it. If i try with findViewById there is no 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>

I have tried to override dialogTitleDecorLayout which is only reference to dialog_title_holo.xml in my theme.xml, but without success. Error is:

error: Error: No resource found that matches the given name: attr 'dialogTitleDecorLayout'.

15条回答
我想做一个坏孩纸
2楼-- · 2019-01-14 22:06

Do you watchthis and there is a pcecial library for that, you can watch it there. And the last link will solve you problem

查看更多
祖国的老花朵
3楼-- · 2019-01-14 22:12

These is no way hiding it by control brotha.. I've had the same problem. only thing you can do is create your own CustomDialog

Here is a sample App

Download and have look at the design pattern, then it will be easy

Here is one Tutorial About making Custom Dialog

Important part is after creating the DialogObject don't set the Title by setTitle() create TextView inside your CustomLayout and call it from findViewByID() and set your title

查看更多
Bombasti
4楼-- · 2019-01-14 22:14

Your idea was correct. However, dialogTitleDecorLayout you were looking for is a private resource, so you can't access it in a normal way. But you still can access it using * syntax:

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

Adding this to my own style and simply copying dialog_title.xml to my app and changing it slightly solved the problem in my case.

查看更多
\"骚年 ilove
5楼-- · 2019-01-14 22:14

In colors.xml:

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

In dialog:

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

View divider = d.findViewById(divierId); divider.setBackgroundColor(getResources().getColor(R.color.transparent));

查看更多
唯我独甜
6楼-- · 2019-01-14 22:19

In order to hide the default blue line completely (assuming you're in 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);
        }
    }
查看更多
我只想做你的唯一
7楼-- · 2019-01-14 22:24

you can make a custom dialog like this:

    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
         }
    });

Set a custom title in layout don't use android

     dialog.setTitle();

and your 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>
查看更多
登录 后发表回答