简体   繁体   中英

Custom DialogPreference & TextView color/theme

I create the custom DialogPreference with this code:

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;

public class DialogPref extends DialogPreference {

    public DialogPref(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.my_dialog);
    }
}

And with this layout:

<?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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dip"
        android:text="@string/dialog_text" />

</LinearLayout>

And when I compile this it shown to me the valid dialog but the TextView on this dialog is black on dark background. So, How to change the TextView color with Dialog theme but not manually in layout? Thanx

You can define a general Theme and set this Theme to the Dialog, passing it through the constructor.

  public DialogPref(Context context, AttributeSet attrs) {
    super(context, attrs,R.style.DialogTheme);

    setDialogLayoutResource(R.layout.my_layout);
}

How to define a Theme : Android Styles and Themes - Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM