2つのImageButtonがあるとします。ここで、状態に基づいて画像を表示するには、各ImageButtonのセレクターxmlを作成する必要があります。両方のビューに同じセレクターを記述できますか?私のアプリには多くのボタンがあり、ビューごとにセレクターxmlを記述しなければならないためです。最適化できますか?
5181 次
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>
(注:コード内の画像を置き換えるため、drawableLeft属性を持つTextViewではなくImageViewとTextViewでLinearLayoutを使用します。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 に答える