Android 在dialogpreference中更改标题视图下蓝线的颜色

Android 在dialogpreference中更改标题视图下蓝线的颜色,android,dialog,settings,color-scheme,preference,Android,Dialog,Settings,Color Scheme,Preference,有没有办法将DialogPreference标题下的蓝线颜色更改为黄色或橙色?我试过: 手动更改AlertDialogs的主题 使用project:android风格的对话框库 子类化DialogPreference并添加一个customTitleView,该视图在onPrepareDialogBuilder(Builder)中带有一个2 dp宽的橙色绘图表。(见附件) 这些尝试都没有成功。有没有人有过这方面的经验并找到了解决方案 好吧,我想出来了,但这是一个很难解决的问题。对于DialogPr

有没有办法将DialogPreference标题下的蓝线颜色更改为黄色或橙色?我试过:

  • 手动更改AlertDialogs的主题
  • 使用
    project:android风格的对话框
  • 子类化DialogPreference并添加一个
    customTitleView
    ,该视图在
    onPrepareDialogBuilder(Builder)
    中带有一个2 dp宽的橙色绘图表。(见附件)

  • 这些尝试都没有成功。有没有人有过这方面的经验并找到了解决方案

    好吧,我想出来了,但这是一个很难解决的问题。对于DialogPreferences,在showDialog()之前不会调用AlertDialog.Builder方法(请参阅)

    知道这一点并从中修改解决方案,我们可以使用 Window.FEATURE_NO_TITLE标志,并使用标题和我们自己的水平分隔符来扩展我们自己的XML:

    @Override
        protected void showDialog(Bundle state) {
            Context context = getContext();        
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
            View view = LayoutInflater.from(context).inflate(R.layout.pref_dialog_about_title, null);
    
            onBindDialogView(view);
            mBuilder.setView(view);
            onPrepareDialogBuilder(mBuilder);
    
    //        getPreferenceManager().registerOnActivityDestroyListener();
    
            // Create the dialog
            final Dialog dialog = mBuilder.create();
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            if (state != null) {
                dialog.onRestoreInstanceState(state);
            }
    
            dialog.setOnDismissListener(this);
            dialog.show();
    
            Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL);
            FontChanger.getInstance(getContext()).changeFont(neutralButton);
            neutralButton.setTextColor(mColorOrangeEnabled);
            neutralButton.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ab_background_gradient_bottom));
        }
    
    其中,
    onBindDialogView(视图视图)
    只需定位文本视图(标题和消息)并对其进行初始化
    onPrepareDialogBuilder()
    只调用setNeutralButton(…)

    pref_dialog_about_title.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/title_template"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:background="@drawable/ab_background_gradient_top">
            <TextView 
                android:id="@+id/alertTitle"
                style="?android:attr/textAppearanceLarge"
                android:layout_marginTop="6dip"
                android:layout_marginBottom="9dip"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/border">
    
                <TextView
                    android:id="@+id/author"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"/>
                <TextView
                    android:id="@+id/btag"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/author"
                    android:layout_centerHorizontal="true"/>
    
            </RelativeLayout>
    </LinearLayout>
    
    
    
    ab_背景_梯度_顶部:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Bottom Line -->
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/orange" />
            </shape>
        </item>
    
        <!-- Color of your action bar -->
        <item android:bottom="2dip">
            <shape android:shape="rectangle">
                <gradient
                android:angle="90"
                android:centerColor="#3b0808"
                android:endColor="#570b0b"
                android:startColor="#1d0404"
                android:type="linear" />
    
                <corners 
                    android:radius="0dp"/>
            </shape>
        </item>
    </layer-list>
    

    的可能重复,它有一个很好的答案已经尝试过这个和约瑟夫·厄尔的解决方案。不起作用。