Android 使用自定义布局获取首选项视图
我喜欢android:布局gft。 我已经在xml文件中首选项的android:layout属性中设置了布局 现在我需要在布局中初始化一个按钮(它是+1按钮)。 我需要在onCreate方法中获取该按钮(它是+1按钮),但是当我使用Android 使用自定义布局获取首选项视图,android,android-preferences,Android,Android Preferences,我喜欢android:布局gft。 我已经在xml文件中首选项的android:layout属性中设置了布局 现在我需要在布局中初始化一个按钮(它是+1按钮)。 我需要在onCreate方法中获取该按钮(它是+1按钮),但是当我使用findviewbyd(button\u id)时,它返回null 有什么方法可以了解这种偏好吗 更新:在添加首选项xml后,似乎没有创建首选项,因此得到空值。我可以在哪里调用findViewById以便按钮已经创建 谢谢 首选项xml: <Preference
findviewbyd(button\u id)
时,它返回null
有什么方法可以了解这种偏好吗
更新:在添加首选项xml后,似乎没有创建首选项,因此得到空值。我可以在哪里调用findViewById以便按钮已经创建
谢谢
首选项xml:
<Preference android:title="Rate this app"
android:key="rate"
android:widgetLayout="@layout/plusone_pref"/>
</PreferenceScreen>
首选项布局xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.plus.PlusOneButton xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="@+id/plus_one_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
plus:size="standard" />
您需要创建一个扩展
首选项的类
,然后覆盖onBindView
,您就可以访问该视图了
public class MyPreference extends Preference {
...
@Override
protected void onBindView(View view) {
super.onBindView(view);
View myView = (View) view.findViewById(R.id.my_id);
// ... do stuff
}
}
您可能需要重写更多的方法,阅读PreferenceActivity中的自定义首选项-