在 PreferenceActivity 中找不到 TextView
在我的 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
始终为 null。
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"/>
.....
为什么?
In my PreferenceActivity I have some Preferences.
One I inflate from layout:
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
is a TextView from push_reg_status
layout, and push_reg_status_txt
is always null.
push_reg_status layout:
....
<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"/>
.....
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Preference.setLayoutResource()
仅更新首选项对布局 ID 的内部引用,它实际上并不更新布局以重新显示。因此,您使用findViewById()
查找的 TextView 不会膨胀供您使用。首选项唯一会夸大其布局的地方是在创建时。您需要在开始时设置自定义布局(在
addPreferencesFromResource()
膨胀所有内容之前),或者调整现有首选项的标题/摘要属性,而不是设置“注册成功”字符串。另外,如果您使用自定义首选项布局,请确保遵循 SDK 文档。希望有帮助!
Preference.setLayoutResource()
only updates the internal reference that preference has to a layout ID, it does not actually update the layout to redisplay. Therefore the TextView you are looking for withfindViewById()
is not inflated for your use. The only place Preferences inflate their layouts is when created.You'll either need to have that custom layout set at the start (before
addPreferencesFromResource()
inflates everything), or adjust the title/summary properties of the existing preference instead to set your "Registration Successful" string. Also, if you are using a custom preference layout, make sure you're following the rules set forth in the SDK Documentation.Hope that Helps!
也许是因为您调用 Activity 的 findViewById 而不是调用 RegistrationStatus 对象的相同方法。
Maybe because you calls findViewById of your Activity instead of calling same method of registrationStatus object.