在Dialog中设置titleDivider的样式

And*_*raz 18 android dialog divider

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

恼人的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>
Run Code Online (Sandbox Code Playgroud)

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

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

Sha*_*ske 23

感谢所有,但我得到了解决方案,以引用alertdialog的titledivider使用下面的代码更改其颜色.希望这有助于某人.

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

  • 给出空指针异常... divider为null (4认同)

小智 13

你需要实施

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" );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*raz 9

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