0

Preference カテゴリ ヘッダーをアプリのテーマと一致させようとしているので、SO とフォローしているいくつかのブログの両方を検索した後、カスタムの Preference を作成するのが最善の (そして最も簡単な) 方法であることがわかりました。 onCreateView() または onBindView() をオーバーライドし、カテゴリ TextView とその背景の色をプログラムで設定するカテゴリ クラス。簡単に聞こえます。

私の唯一の 2 つの注意点は、SherlockPreferenceActivity を使用していることです。したがって、Sherlock テーマまたは派生物を使用する必要があるため、アクティビティにカスタム テーマを単純に適用することはできません。また、私のもう1つのこと(これが原因かもしれないと思います)は、TextViewの背景をxmlのストロークを持つグラデーションであるShape Drawableに設定しようとしていることです。設定カテゴリの背景は問題なく変更されますが、TextView がまったく表示されないため、背景が textView の上に描画されているのではないかと思いますか? わからない

CustomPreferenceCategory クラスのコードは次のとおりです。ご覧のとおり、onBindView() と onCreateView() の両方を使用して試しました。

public class CustomPreferenceCategory extends PreferenceCategory {



public CustomPreferenceCategory(Context context) {
    super(context);



}

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



}

public CustomPreferenceCategory(Context context, AttributeSet attrs,
        int defStyle) {
    super(context);



}


 @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView titleView = (TextView) view.findViewById(android.R.id.title);
        titleView.setBackgroundResource(R.drawable.header_non_rounded);
        titleView.setTextColor(Color.WHITE);

    }



//@Override
/*protected View onCreateView(ViewGroup parent) {
    // And it's just a TextView!
    TextView categoryTitle = (TextView) super.onCreateView(parent);
    categoryTitle.setBackgroundResource(R.drawable.header_non_rounded);
    categoryTitle.setTextColor(Color.WHITE);



    return categoryTitle;
}*/

}

これが私のpreferences.xmlファイルを介して設定する方法です

<com.brightr.weathermate.views.CustomPreferenceCategory android:title="Weather"      >
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="degreesC"
        android:summary="Display the weather degrees in Celsius units"
        android:title="Show Celsius" />
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="degreesF"
        android:summary="Display the weather in Farenheit units"
        android:title="Show Farenheit" />

    <ListPreference
        android:entries="@array/textColors"
        android:entryValues="@array/textColor_values"
        android:summary="Change the color of the temerature text"
        android:title="Temperature Text Color" />
</com.brightr.weathermate.views.CustomPreferenceCategory>

背景だけが変更されているのに TextView が表示されない理由について何か考えはありますか? また、setBackgroundDrawable() の代わりに setBackgroundResource() を使用しても、テキストは設定されません。どんな助けでも大歓迎です。

4

1 に答える 1

0

とった。愚かな私、各コンストラクターの他の2つのパラメーターを渡すのを忘れました。私は自分を蹴りたい気がします。

public CustomPreferenceCategory(Context context) {
super(context);



}

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



}

public CustomPreferenceCategory(Context context, AttributeSet attrs,
    int defStyle) {
super(context, attrs, defStyle);



}
于 2013-01-30T01:13:29.590 に答える