4

假设有 2 个 ImageButtons。现在,要根据其状态显示图像,我应该为每个 ImageButton 编写一个选择器 xml。我们可以为两个视图编写相同的选择器吗?因为我的应用程序有很多按钮,并且必须为每个视图编写一个选择器 xml。我们可以优化它吗?

4

2 回答 2

6

如果您尝试使用 xml 文件,那么您必须为每个按钮创建每个选择器。

因此,尝试通过继承 StateListDrawable 或尝试以下动态创建选择器的代码来动态执行:::

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
    getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
    getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);
于 2012-04-15T12:25:11.100 回答
0

您不必为每个按钮创建 xml 选择器,只需重新使用适合的选择器即可。我在我的项目中使用以下代码来处理具有相同选择器和其他属性的许多按钮。

<LinearLayout android:id="@+id/group_news" style="@style/main_button_group" >
    <ImageView android:id="@+id/image_news" style="@style/main_button_image" android:src="@drawable/news" />
    <TextView android:id="@+id/text_news" style="@style/main_button_text" android:text="@string/button_news" />
</LinearLayout>

(注意:我使用带有 ImageView 和 TextView 的 LinearLayout 而不是带有 drawableLeft 属性的 TextView,因为我正在替换代码中的图像。更改 drawableLeft 无法正常工作)

然后在 res\values 文件夹中创建一个styles.xml,其中所有按钮的属性都相同,例如:

<resources>
    <style name="main_button_group">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:layout_marginTop">2dp</item>
        <item name="android:paddingTop">10dp</item>
        <item name="android:paddingBottom">10dp</item>
        <item name="android:paddingRight">10dp</item>
        <item name="android:paddingLeft">25dp</item>
        <item name="android:background">@drawable/main_button_states</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
    </style>
    <style name="main_button_image">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center_vertical</item>
        <item name="android:contentDescription">@string/empty</item>
    </style>
    <style name="main_button_text">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_margin">10dp</item>
        <item name="android:layout_gravity">center_vertical</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textColor">@color/general_value</item>
    </style>
</resources>

选择器@drawable/main_button_states如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/main_button_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/main_button_focused" />
    <item android:state_hovered="true" android:drawable="@drawable/main_button_focused" />
    <item android:drawable="@drawable/main_button_normal" />
</selector>

这完美地工作。

于 2012-04-15T14:14:39.167 回答