如何在偏好片段中改变文本样式

时间:2014-05-08 09:26:27

标签: android xml android-layout android-fragments android-preferences

我已经创建了带文本视图的首选项屏幕。用户可以在首选项屏幕中更改文本视图样式,如字体,颜色,大小。因此它将显示在我在偏好设置屏幕中创建的Textview上。但它不起作用。如果偏好Activity,我们可以使用setContentview,在片段中我不知道如何传递视图..

 public final class TestFragment2 extends PreferenceFragment {
static TextView textView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View view = inflater.inflate(R.layout.customprefernce, null);
    textView = (TextView) view.findViewById(R.id.TextViewSample);

    final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager()
            .findPreference("bold");

    checkboxPref
            .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {
                    // Log.d("MyApp", "Pref " + preference.getKey() +
                    // " changed to " + newValue.toString());
                    textView.setTextColor(Color.BLUE);
                    // Color is not changing.

                    return true;
                }
            });

}

我有选中颜色的复选框,但不改变颜色                 textView.setTextColor(Color.BLUE);

Preferncescreen.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:holo="http://schemas.android.com/apk/res-auto">
<Preference

    android:selectable="false"
    android:summary="@string/preference_summary"
    android:title="@string/preference_title" />

<Preference

android:layout="@layout/customprefernce" />
<PreferenceCategory android:title="@string/preference_base">

    <CheckBoxPreference
        android:id="@+id/bold"
        android:defaultValue="false"
        android:key="bold"

        android:title="@string/preference_check_box_title" />
    <CheckBoxPreference
        android:id="@+id/italic"
        android:key="italic"
        android:defaultValue="false"
        android:summaryOff="@string/preference_check_box_summary_off"
        android:summaryOn="@string/preference_check_box_summary_on"
        android:title="@string/preference_check_box_title" />





</PreferenceCategory>


  </PreferenceScreen>

CustomPrefernce.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
   android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="6dip"
    android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip"
    android:layout_weight="1">

    <TextView android:id="@+id/TextViewSample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Example Tex"

        android:ellipsize="marquee"
        android:fadingEdge="horizontal" />


</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" />

 </LinearLayout>

0 个答案:

没有答案