【问题标题】:State maintainance for a button (Resource handling) in Android?Android中按钮(资源处理)的状态维护?
【发布时间】: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)图像。

我有两种想法,但我不知道如何实现它们。它们是:

  1. 我们必须定义一个方法而不是选择器文件。
  2. 我们应该有一个图像,我们需要像 iPhone 一样更改 alpha 值。

是否最需要任何其他伟大的想法。因为这会增加应用程序的大小。

请表达您的想法和想法。

【问题讨论】:

  • 好吧,与其为每个密度/屏幕尺寸制作新图像,为什么不考虑使用 NinePatch?见:developer.android.com/reference/android/graphics/NinePatch.html
  • @willytate 噢,如果 9patch 无论如何都会调整它的大小,我们为什么还要使用 /drawable-hdpi/ 等?如果您将评论作为答案,我将不胜感激:D
  • 回答我自己的问题,使用 9 个补丁,图像的“角”永远不会变大,只有边会拉伸。这是您将使用 /drawable-hdpi/ 的场景

标签: android button selector drawable


【解决方案1】:

我猜,您按钮的背景并不太复杂。如果不是,那么您可以使用 9-patch png,因此,可绘制对象的数量将减少到只有一组。 9-patch png 将根据需要调整大小和缩放。

但是,如果背景很复杂,则每个分辨率都需要多个资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2023-03-30
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多