我在我的首选项屏幕中添加了一个小部件布局,显示了一个箭头图标,以向用户表明给定的首选项屏幕后面还有更多首选项。
但是,我注意到当未启用 PreferenceScreen 时,它会变暗。有没有办法我也可以更改正在使用的小部件布局,或者使其有状态,以便在禁用 PreferenceScreen 时可以使用褪色的图标?类似于如何将状态列表可绘制应用为按钮的背景?
tl;dr:如何更改禁用 PreferenceLayout 实例时显示的图标?
最佳答案
只需使用 XML,只需 3 个步骤即可完成此操作。
首先,创建一个有状态的drawable,它可以根据使用它的小部件是否启用、禁用等而变化。因此statelist_example.xml保存在drawable文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/expander_ic_minimized"
android:state_enabled="true" />
<item
android:drawable="@drawable/expander_ic_minimized_faded"
android:state_enabled="false" />
</selector>
然后定义您自己的布局以在布局文件夹中使用此可绘制对象。这将成为首选项的“widgetLayout”。所以layout_example.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/widget_frame"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:src="@drawable/statelist_example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_gravity="center" />
</LinearLayout>
第三,在每个首选项中指定 widgetLayout 以使用此布局:
<Preference
android:key="preference_example"
android:title="@string/preference_title"
android:widgetLayout="@layout/layout_example" />
所以基本上你的 Preference 引用了一个引用有状态可绘制对象的布局。
关于android - PreferenceScreen 的状态列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857541/