2011-04-18 74 views
2

在我的PreferenceActivity中,我有一些首选项。 一个我从布局膨胀:在PreferenceActivity中找不到TextView

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_txtpush_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"/> 
..... 

为什么?

回答

0

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

+0

registrationStatus是首选项。 而push_reg_status_txt是布局中的TextView,它扩大了此首选项。 如何调用findViewById作为首选? – vsvydenko 2011-04-18 14:11:16

3

Preference.setLayoutResource()仅更新内部参考的是偏爱有一个布局ID,它实际上并没有更新的布局,重新显示。因此,您用findViewById()寻找的TextView不会被夸大用于您的使用。唯一的地方首选项膨胀他们的布局是创建时。

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

希望有助于!