在DialogFragment android中更改标题的背景颜色

时间:2015-09-18 19:37:50

标签: android dialog fragment android-dialogfragment

我有DialogFragment,它显示了一些信息。它的工作很好,但我需要不同的标题,我需要白色标题颜色文本和标题的蓝色背景。 这是我的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;
    }
    }

我不知道如何改变它,也许你可以帮助我。

1 个答案:

答案 0 :(得分:0)

用于自定义布局:

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

AlertDialog dialog = 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));

自定义标题的背景稍微复杂一些......您需要在主题中定义一个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>