动态更改android首选项';widgetlayout资源
我在preference屏幕中创建了一个preference条目,如下所示:动态更改android首选项';widgetlayout资源,android,android-preferences,preferenceactivity,Android,Android Preferences,Preferenceactivity,我在preference屏幕中创建了一个preference条目,如下所示: <PreferenceScreen> <Preference android:title="title" android:key="key" android:widgetLayout="@layout/someview"/> </PreferenceScreen> Preference myPreference = findP
<PreferenceScreen>
<Preference
android:title="title"
android:key="key"
android:widgetLayout="@layout/someview"/>
</PreferenceScreen>
Preference myPreference = findPreference("key");
myPreference.setWidgetLayoutResource(R.layout.someview);
这两种方法都很好,因此我可以在首选项的右侧看到我的资源。
但是,我无法访问此资源(someview)以在运行时更改其属性
手动设置资源id、从资源中膨胀资源id或findViewById
似乎都不起作用-我有无效的资源/资源id未找到异常。似乎偏好活动会在稍后的某个时间膨胀资源
有人遇到过同样的问题吗?关于如何在运行时更改widgetlayout
资源的属性,有什么想法吗
这里有一个类似的问题,但没有得到回答
- 我似乎也有同样的问题,我试着这样做:
<PreferenceScreen>
<Preference
android:title="title"
android:key="key"
android:widgetLayout="@layout/someview"/>
</PreferenceScreen>
Preference myPreference = findPreference("key");
myPreference.setWidgetLayoutResource(R.layout.someview);
1) 创建自定义的首选项
并覆盖onBindView()
:
2) 将此首选项添加到屏幕xml:
然而,一年多了 我有一个解决这个问题的部分方法。如果目标只是在小部件中显示或隐藏一个不确定的进度条,那么诀窍就是在代码中设置并清除小部件布局资源 1) 定义包含ProgressBar的小部件布局。无需分配ID。首选项_widget_progressbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
获取指向实际ProgressBar的指针非常困难,特别是当首选项位于库项目中时。我根据成功地使用了getIdentifier()
,但这种方法实际上隐藏了整个小部件,而在ProgressBar上设置可见性会使一个空白小部件可见。这里有一个解决方法:
通过设置onClick属性创建小部件布局
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/icon"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="?android:attr/selectableItemBackground"
*android:onClick="onMyKeyClicked"*
android:scaleType="centerInside"
android:src="@drawable/ic_action_info" />
因此,对于具有小部件的每个首选项,您将拥有一个布局,该布局将onClick属性设置为活动中的唯一方法。你觉得怎么样
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<package.ProgressBarPreference
android:key="key"
android:summary="summary"
android:title="title" />
public void showProgressBar(boolean isVisible) {
// Show or hide the entire widget (which contains only a ProgressBar)
setWidgetLayoutResource(isVisible ? R.layout.preference_widget_progressbar: 0);
notifyChanged(); // Update screen
}
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/icon"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="?android:attr/selectableItemBackground"
*android:onClick="onMyKeyClicked"*
android:scaleType="centerInside"
android:src="@drawable/ic_action_info" />
<ListPreference
android:defaultValue="1"
android:entries="@array/my_entries"
android:entryValues="@array/my_values"
android:key="my_key"
android:title="@string/pref_my_key_title"
android:widgetLayout="@layout/pref_layout_my_key" />
public void onMyKeyClicked(View v) {
Toast.makeText(this, "My key widget clicked",
Toast.LENGTH_SHORT).show();
}