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条回答
一纸荒年 Trace。
2楼-- · 2019-01-14 22:00

Here is how I resolved that (thanks to http://joerg-richter.fuyosoft.com/?p=181 ):

MyDialogBuilder.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;
}
}
查看更多
来,给爷笑一个
3楼-- · 2019-01-14 22:02

You need to implement

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" );
        }
    }
}
查看更多
神经病院院长
4楼-- · 2019-01-14 22:02

use

 <View android:id="@+id/titleDivider"
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background=#CC3232 />
查看更多
Luminary・发光体
5楼-- · 2019-01-14 22:03

I solved the issue by using DialogFragment.STYLE_NO_TITLE theme and then faking title bar in dialog layout.

查看更多
别忘想泡老子
6楼-- · 2019-01-14 22:04

"Removing the blue line" if I guess correctly means dropping the border between the title of the dialog and it's body. That border come from the Holo theme, so it's not possible to drop it without using your custom layout.

Create a file named custom-dialog.xml with the following content (it's just an example..modify it as you want):

<?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>

As you see I'm using resources and stuff that won't be in your project, but you can remove them safely. The result in my case is more or less the following one, with an image at top that I'll programatically set in the code.

simple screenshot

To create the dialog then use something like:

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;
}
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-14 22:06

This one is tested on some 4.x devices:

    TextView title = (TextView)getWindow().getDecorView().findViewById(android.R.id.title);
    ((ViewGroup)title.getParent()).getChildAt(1).setVisibility(View.GONE);
查看更多
登录 后发表回答