在preferenceActivity找不到的TextView [英] Cannot find TextView in PreferenceActivity

查看:280
本文介绍了在preferenceActivity找不到的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的preferenceActivity我有一些preferences。
一个我从布局膨胀:

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 push_reg_status 布局和 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"/>
.....

为什么?

推荐答案

preference.setLayoutResource()仅更新内部参考的preference有一个布局ID,它实际上并不更新的布局重新显示。因此,你与 findViewById()寻找的TextView 未充气供您使用。唯一的地方preferences夸大其布局时创建。

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 with findViewById() is not inflated for your use. The only place Preferences inflate their layouts is when created.

你要么需要有一个自定义布局设置在启动(之前添加preferencesFromResource()膨胀一切),或调整标题/摘要属性现有的preference,而不是设置你的注册成功字符串。此外,如果您使用的是自定义的preference布局,确保你在之后的<一个规定的规则href=\"http://developer.android.com/reference/android/$p$pference/$p$pference.html#setLayoutResource%28int%29\"相对=nofollow> SDK文档。

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.

希望帮助!

这篇关于在preferenceActivity找不到的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆