更改 DialogFragment android 中 header 的背景颜色

Change background color of header in DialogFragment android

我有 DialogFragment,它显示了一些信息。效果很好,但我需要不同的 header,我需要 header 的白色标题颜色文本和蓝色背景。 这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/icon_teacher"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/teacher"
                android:id="@+id/teacher"
                android:layout_marginLeft="10dp"
                android:layout_alignTop="@+id/icon_teacher"
                android:layout_toRightOf="@+id/icon_teacher"
                android:layout_toEndOf="@+id/icon_teacher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name_of_teacher"
                android:textColor="@android:color/black"
                android:textSize="16dp"
                android:id="@+id/teacher_name"
                android:layout_below="@+id/teacher"
                android:layout_alignLeft="@+id/teacher"
                android:layout_alignStart="@+id/teacher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/icon_time"
                android:layout_below="@+id/icon_teacher"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/teacher"
                android:id="@+id/time"
                android:layout_marginLeft="10dp"
                android:layout_alignTop="@+id/icon_time"
                android:layout_toRightOf="@+id/icon_time"
                android:layout_toEndOf="@+id/icon_time" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name_of_teacher"
                android:textColor="@android:color/black"
                android:textSize="16dp"
                android:id="@+id/time_name"
                android:layout_below="@+id/time"
                android:layout_alignLeft="@+id/time"
                android:layout_alignStart="@+id/time" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/icon_place"
                android:layout_below="@+id/icon_time"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/teacher"
                android:id="@+id/place"
                android:layout_marginLeft="10dp"
                android:layout_alignTop="@+id/icon_place"
                android:layout_toRightOf="@+id/icon_place"
                android:layout_toEndOf="@+id/icon_place" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name_of_teacher"
                android:textColor="@android:color/black"
                android:textSize="16dp"
                android:id="@+id/place_name"
                android:layout_below="@+id/place"
                android:layout_alignLeft="@+id/place"
                android:layout_alignStart="@+id/place" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/icon_home"
                android:layout_below="@+id/icon_place"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/teacher"
                android:id="@+id/home"
                android:layout_marginLeft="10dp"
                android:layout_alignTop="@+id/icon_home"
                android:layout_toRightOf="@+id/icon_home"
                android:layout_toEndOf="@+id/icon_home" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name_of_teacher"
                android:textColor="@android:color/black"
                android:textSize="16dp"
                android:id="@+id/place_home"
                android:layout_below="@+id/home"
                android:layout_alignLeft="@+id/home"
                android:layout_alignStart="@+id/home" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:id="@+id/divider1"
                android:layout_below="@+id/icon_home"
                android:layout_marginTop="10dp"
                android:background="@android:color/darker_gray"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/divider1"
                android:layout_marginTop="10dp">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher"
                    android:layout_marginLeft="10dp"
                    android:id="@+id/imageView" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/alert"
                    android:id="@+id/alert"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center_vertical"/>
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

还有我的 DialogFragment,但我认为它不会有太大帮助:

public class LessonDialogFragment extends DialogFragment {

    View view;
    String title;

    public LessonDialogFragment(String title) {
        this.title = title;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.detail_dialog_fragment, container, false);
        getDialog().setTitle(title);
        return view;
    }
    }

我不知道怎么改,也许你能帮我。

自定义布局:

https://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

AlertDialog 对话框 = builder.show();

// Set title divider color
int titleDividerId = getResources().getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.findViewById(titleDividerId);
if (titleDivider != null)
    titleDivider.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));

自定义 header 的背景稍微复杂一些...您需要在主题中定义一个 alertDialogStyle,定义如何绘制对话框的每个区域。例如:

<style name="Theme.Yours" parent="@android:style/Theme.Holo">
    ...
    <item name="android:alertDialogStyle">@style/AlertDialog_Yours</item>
</style>

<style name="AlertDialog_Yours">
    <item name="android:fullDark">...</item>
    <item name="android:topDark">...</item>
    <item name="android:centerDark">...</item>
    <item name="android:bottomDark">...</item>
    <item name="android:fullBright">...</item>
    <item name="android:topBright">...</item>
    <item name="android:centerBright">...</item>
    <item name="android:bottomBright">...</item>
    <item name="android:bottomMedium">...</item>
    <item name="android:centerMedium">...</item>
</style>