gpt4 book ai didi

android - 如何在 Android 中引用系统选择按钮图像以进行自定义首选项

转载 作者:搜寻专家 更新时间:2023-11-01 09:15:51 25 4
gpt4 key购买 nike

我有一个客户偏好小部件 (xml),它基本上包含一个 TextView,旁边是一个右对齐的 ImageButton。

我想做的是将图像按钮的图像设置为首选项系统默认使用的图像。例如,请参阅附件图像,其中我突出显示了我想要使用的图像类型。

我试过在线查看并将 ic_btn_round 图像保存到我的可绘制对象中并使用它,但它看起来并不正确。

欢迎大家多多指教,谢谢

alt text

最佳答案

这个圆圈按钮来自DialogPreference .如果我们研究这个首选项是如何实现的,我们会发现,它使用了这个 xml 文件中描述的小部件:

<!-- Layout used by DialogPreference widgets. This is inflated inside 
android.R.layout.preference. -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dip"
android:layout_gravity="center_vertical"
android:background="@drawable/btn_circle"
android:src="@drawable/ic_btn_round_more" />

因此您需要查看另外 2 个文件:drawable/btn_circle 和 drawable/ic_btn_round_more。

btn_cicle.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_circle_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_circle_disable" />
<item android:state_pressed="true" android:state_enabled="false"
android:drawable="@drawable/btn_circle_disable" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_circle_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_circle_selected" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_circle_normal" />
<item android:state_focused="true"
android:drawable="@drawable/btn_circle_disable_focused" />
<item
android:drawable="@drawable/btn_circle_disable" />
</selector>

ic_btn_round_more.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="@drawable/ic_btn_round_more_disabled" />
<item
android:drawable="@drawable/ic_btn_round_more_normal" />
</selector>

从这两个 xml 文件引用的所有可绘制对象都是实际的 *.png 文件。

所以基本上你需要使用两个图像来达到预期的效果。但是所有这些资源都是Android内部的,你不能直接使用它们。最简单的方法是将它们复制到您的项目中(就像您对 ic_btn_round 所做的那样)。

关于android - 如何在 Android 中引用系统选择按钮图像以进行自定义首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4738249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com