Android: how to set the same drawable selector xml for more than 2 views

Suppose there are 2 ImageButtons. Now, to display an image based on its state, I have to write an xml selector for each of ImageButton. Can we write the same selector for both views. Since my application has many buttons and for each view you need to write an xml selector. Can we optimize it?

+3
source share
2 answers

If you try it with an xml file, you must create each selector for each button.

So, try to do it dynamically either by subclassing StateListDrawable, or try the code below that creates the selector dynamically:

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);
+6

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>

(: LinearLayout ImageView TextView, TextView drawableLeft, . drawableLeft )

styles.xml res\values ​​ , , :

<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>

.

0

All Articles