2

在我的 PreferenceActivity 中,我有一些偏好。我从布局中膨胀的一个:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   addPreferencesFromResource(R.layout.settings);
   ...
}

protected void onResume() {     
    super.onResume();
    Preference registrationStatus = (Preference) findPreference("registrationStatusPref");
    if (!TextUtils.isEmpty(somestring){
       registrationStatus.setLayoutResource(R.layout.push_reg_status);
       TextView push_reg_status_txt = (TextView)findViewById(R.id.pushRegTxtPref);
    }
    ....
}

push_reg_status_txt是来自push_reg_status布局的 TextView,并且push_reg_status_txt 始终为空。

push_reg_status布局:

....
<TextView
        android:id="@+id/pushRegTxtPref"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="4dp"
        android:text="Registration Succesful"
        android:textColor="#99ff33"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"/>
.....

为什么?

4

2 回答 2

3

Preference.setLayoutResource()仅更新首选项对布局 ID 的内部引用,它实际上并没有更新布局以重新显示。因此,您正在寻找的 TextViewfindViewById()不会为您的使用而膨胀。首选项膨胀其布局的唯一地方是在创建时。

您要么需要在开始时设置自定义布局(在addPreferencesFromResource()膨胀所有内容之前),要么调整现有首选项的标题/摘要属性,而不是设置“注册成功”字符串。此外,如果您使用自定义首选项布局,请确保您遵循SDK 文档中规定的规则。

希望有帮助!

于 2011-04-18T14:43:22.897 回答
0

可能是因为您调用了 Activity 的 findViewById 而不是调用 registrationStatus 对象的相同方法。

于 2011-04-18T12:43:25.950 回答