PreferenceScreen 的状态列表?
我在我的首选项屏幕中添加了一个小部件布局,显示了一个箭头图标,以向用户表明给定的 PreferenceScreen 后面还有更多首选项。
但是,我注意到当未启用 PreferenceScreen 时,它会变暗。有没有办法我也可以更改正在使用的小部件布局,或者使其有状态,以便在禁用 PreferenceScreen 时可以使用褪色的图标?类似于如何将状态列表可绘制应用为按钮的背景?
tl;dr: 如何更改禁用 PreferenceLayout 实例时显示的图标?
I've added a widget layout to my preference screens showing an arrow icon, to indicate to users that there are more preferences behind a given PreferenceScreen.
However, I notice when a PreferenceScreen is not enabled it is darkened. Is there a way I could also change the widget layout being used, or make it stateful, so that I could use a faded icon when my PreferenceScreen is disabled? Similar to how a state list drawable can be applied as a button's background?
tl;dr: How can I change the icon displayed when an instance of PreferenceLayout is disabled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用 XML,只需 3 个步骤即可完成此操作。
首先,创建一个有状态的可绘制对象,它可以根据使用它的小部件是否启用、禁用等进行更改。因此 statelist_example.xml 保存在可绘制文件夹中:
然后在布局文件夹中定义您自己的布局以使用此可绘制对象。这将成为首选项的“widgetLayout”。所以layout_example.xml:
第三,在每个首选项中指定widgetLayout以使用此布局:
所以基本上您的首选项引用了一个引用有状态可绘制的布局。
关于 Statelist Drawable 的 Android 文档。
关于首选项 widgetLayout 的 Android 文档。
This can be done solely with XML in 3 steps.
First, create a stateful drawable that can change according to whether the widget using it is enabled, disabled, etc. So statelist_example.xml kept in the drawable folder:
Then define your own layout to use this drawable in the layouts folder. This will become the "widgetLayout" for the Preference. So layout_example.xml:
Third, specify the widgetLayout in every preference to use this layout:
So basically you have your Preference reference a Layout which references a stateful drawable.
Android Docs on Statelist Drawable.
Android Docs on Preference widgetLayout.
好方法
我认为解决这个问题的最好方法是创建您自己的类来扩展 Preference/PreferenceScreen。您想要的布局资源在类的初始化中调用:
它创建要在小部件区域中显示的视图,我认为禁用首选项时该视图不会变灰。
黑客/丑陋的方法
恐怕我只是想出了一个解决这个问题的办法,我不相信小部件支持状态。
只需以编程方式更改小部件:
然后在每次调用依赖项时更改小部件布局(有点混乱)。
Nice method
The best way I can see to tackle this problem is to create your own class which extends a Preference/PreferenceScreen. The layout resource you want is called in the initialisation of the class:
Which creates the view to be displayed in the widget area, which I don't think get grayed out when the preference is disabled.
Hacky/Ugly Method
Afraid I've only come up with a hack around this issue, I don't believe the widgets support states.
Just change the widget programatically:
Then change the widget layout when each time a dependency is called (kinda messy).