【发布时间】:2011-05-30 11:36:48
【问题描述】:
我已经实现了使用选择器 XML 组件来维护按钮的状态。以代码 sn -p 为例。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states
-->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/back_arrow_normal" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" />
<!-- Focused states
-->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/back_arrow_hover" />
<!-- Pressed
-->
<item android:state_pressed="true" android:drawable="@drawable/back_arrow_hover" />
</selector>
使用这个想法,我们应该至少有两个图像。如果我在一个项目中有十个按钮,那么我应该在资源 drawable 中有二十个图像。为了使应用程序支持多种密度/屏幕尺寸,我需要 60 个(20X3)图像。
我有两种想法,但我不知道如何实现它们。它们是:
- 我们必须定义一个方法而不是选择器文件。
- 我们应该有一个图像,我们需要像 iPhone 一样更改 alpha 值。
是否最需要任何其他伟大的想法。因为这会增加应用程序的大小。
请表达您的想法和想法。
【问题讨论】:
-
好吧,与其为每个密度/屏幕尺寸制作新图像,为什么不考虑使用 NinePatch?见:developer.android.com/reference/android/graphics/NinePatch.html
-
@willytate 噢,如果 9patch 无论如何都会调整它的大小,我们为什么还要使用 /drawable-hdpi/ 等?如果您将评论作为答案,我将不胜感激:D
-
回答我自己的问题,使用 9 个补丁,图像的“角”永远不会变大,只有边会拉伸。这是您将使用 /drawable-hdpi/ 的场景
标签: android button selector drawable