簡體   English   中英

在Android中設置自定義對話框的操作欄顏色

[英]Set the Action bar color of a custom Dialog in android

我正在為我的應用程序設計自定義對話框。 我在設置“自定義對話框”的標題背景顏色時遇到困難。 我已經提到了一個Stack Overflow帖子,他們要求我使用一種名為dialog.getWindow().setTitleColor(color) 但是不建議使用此方法,並且未設置標題顏色。 發布到目前為止我嘗試過的代碼。

styles.xml

    <style name="custom_theme_dialog" parent="android:Theme.DeviceDefault.Light">
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowBackground">@color/offWhite</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>

dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/photo"
    android:textColor="@color/Gallery"
    android:drawableStart="@drawable/ic_perm_media_green_24dp"
    android:textSize="24sp"
    android:drawablePadding="10dp"
    android:layout_marginTop="20dp"
    android:layout_marginStart="24dp" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0">

<android.support.design.widget.TextInputLayout
    android:id="@+id/eventNameTextInputLayout"
    style="@style/textfieldbox"
    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.8"
    android:layout_marginTop="15dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/eventNameTextInputEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:drawablePadding="15dp"
        android:textColor="@color/textInputEditTextColor"
        android:drawableStart="@drawable/ic_event_black_24dp"
        android:hint="@string/eventName"
        android:alpha="0.5"
        android:inputType="textEmailAddress"
        tools:ignore="MissingPrefix,UnusedAttribute" />

</android.support.design.widget.TextInputLayout>

    <ImageView
        android:id="@+id/recordEventName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_mic_black_24dp"
        android:contentDescription="@string/galleryLabel"
        android:tint="@color/Gallery"
        android:layout_marginEnd="10dp"
        android:layout_weight="0.1"
        android:layout_marginStart="10dp"
        android:layout_marginTop="30dp"
        android:onClick="recordDescription"/>

    <ImageView
        android:id="@+id/playEventName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/galleryLabel"
        android:layout_weight="0.1"
        android:tint="@color/Gallery"
        android:layout_marginTop="30dp"
        android:src="@drawable/ic_volume_up_black_48dp"
        android:onClick="onClick"/>
    </LinearLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/eventDateTextInputLayout"
    style="@style/textfieldbox"
    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/eventDateTextEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:drawablePadding="15dp"
        android:focusable="false"
        android:textColor="@color/textInputEditTextColor"
        android:drawableStart="@drawable/ic_date_range_black_24dp"
        android:hint="@string/eventDate"
        android:alpha="0.5"
        android:inputType="textEmailAddress"
        tools:ignore="MissingPrefix,UnusedAttribute" />

</android.support.design.widget.TextInputLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/shortDescriptionDialogTextInputLayout"
        style="@style/textfieldbox"
        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:layout_marginTop="15dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/shortDescriptionDialogTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:drawablePadding="15dp"
            android:textColor="@color/textInputEditTextColor"
            android:drawableStart="@drawable/ic_attach_file_green_24dp"
            android:hint="@string/shortDesc"
            android:alpha="0.5"
            android:inputType="textEmailAddress"
            tools:ignore="MissingPrefix,UnusedAttribute" />

    </android.support.design.widget.TextInputLayout>

    <ImageView
        android:id="@+id/recordShortDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_mic_black_24dp"
        android:contentDescription="@string/galleryLabel"
        android:tint="@color/Gallery"
        android:layout_marginEnd="10dp"
        android:layout_weight="0.1"
        android:layout_marginStart="10dp"
        android:layout_marginTop="30dp"
        android:onClick="recordDescription"/>

    <ImageView
        android:id="@+id/playShortDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/galleryLabel"
        android:layout_weight="0.1"
        android:tint="@color/Gallery"
        android:layout_marginTop="30dp"
        android:src="@drawable/ic_volume_up_black_48dp"
        android:onClick="onClick"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/longDescriptionDialogTextInputLayout"
        style="@style/textfieldbox"
        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:layout_marginTop="15dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/longDescriptionDialogTextInputEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:drawablePadding="15dp"
            android:textColor="@color/textInputEditTextColor"
            android:drawableStart="@drawable/ic_attach_file_green_24dp"
            android:hint="@string/longDesc"
            android:alpha="0.5"
            android:inputType="textEmailAddress"
            tools:ignore="MissingPrefix,UnusedAttribute" />

    </android.support.design.widget.TextInputLayout>

    <ImageView
        android:id="@+id/recordLongDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_mic_black_24dp"
        android:contentDescription="@string/galleryLabel"
        android:tint="@color/Gallery"
        android:layout_marginEnd="10dp"
        android:layout_weight="0.1"
        android:layout_marginStart="10dp"
        android:layout_marginTop="30dp"
        android:onClick="recordDescription"/>

    <ImageView
        android:id="@+id/playLongDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/galleryLabel"
        android:layout_weight="0.1"
        android:tint="@color/Gallery"
        android:layout_marginTop="30dp"
        android:src="@drawable/ic_volume_up_black_48dp"
        android:onClick="onClick"/>
</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/okButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginStart="50dp"
    android:src="@drawable/ic_done_green_24dp"
    app:backgroundTint="@color/loginbackground"
    android:translationZ="4dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="15dp"
    app:fabSize="normal" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/cancelButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:src="@drawable/ic_close_black_24dp"
    android:tint="@color/Gallery"
    android:layout_marginStart="80dp"
    app:backgroundTint="@color/loginbackground"
    android:translationZ="4dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="15dp"
    app:fabSize="normal" />

</FrameLayout>


</LinearLayout>

dialog.java

dialogAddMemory = new Dialog(PhotosActivity.this, R.style.custom_theme_dialog);
                        dialogAddMemory.setTitle(getString(R.string.memoryInfo));
                        dialogAddMemory.setContentView(R.layout.dialog_gallery_info);
                        dialogAddMemory.show();

我認為使用您自己的標題而不是覆蓋默認對話框標題很容易。

使用此代碼禁用對話框標題

dialogAddMemory.requestWindowFeature(Window.FEATURE_NO_TITLE);

並為標題添加另一個textview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM