android - PreferenceScreen 的状态列表?

标签 android button android-widget preferencescreen

我在我的首选项屏幕中添加了一个小部件布局,显示了一个箭头图标,以向用户表明给定的首选项屏幕后面还有更多首选项。

但是,我注意到当未启用 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 Docs on Statelist Drawable.

Android Docs on Preference widgetLayout.

关于android - PreferenceScreen 的状态列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857541/

相关文章:

java - 使用 Endless 适配器的 ListView

dart - Flutter:是否有可能创建 App Widgets (Android) 和 Today Extensions (iOS)?

android - 在 Android 中使用 Canvas.drawText 呈现清晰的文本

一旦用户添加了文本,Javascript 按钮就停止写入文本区域

button - JBoss Seam 使用按钮取消请求

android - 如何设置带有背景颜色的圆形按钮并在按下时更改颜色

android - 多个 Android Widget 实例只更新最后一个 widget

android - Qpython 中的 OpenCV

android - 缺少 API 和身份验证 > 在 Google 云控制台中注册的应用程序/如何获取 GCM API key ?

java - SonarQube:如何将多个质量配置文件应用于一个项目?