2012-04-15 1 views
3

ImageButtonが2つあるとします。その状態に基づいてイメージを表示するには、各ImageButtonにセレクタxmlを書き込む必要があります。両方のビューに対して同じセレクターを記述できますか?私のアプリケーションにはたくさんのボタンがあり、ビューごとにセレクターxmlを記述する必要があるためです。最適化できますか?Android:2つ以上のビューに対して同じdrawable selector xmlを設定する方法

答えて

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

解決に感謝します。 –

+0

素晴らしい作品です。また、XMLの場合と同様に、状態を結合したり否定することができます( " - ")。 so pressed = true checked = falseは次のように変換されます:states.addState(new int [] {android.R.attr.state_pressed、-android.R.attr.state_checked});参照:http://stackoverflow.com/questions/5092649 –

+0

素晴らしい..! andriod.R.attr。*で利用可能な状態のリストがありますか? –

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が正常に動作していなかった変更だので、私は、ImageViewのとdrawableLeft属性を持つのTextViewとないのTextViewでのLinearLayoutを使う)

そして解像度でのstyles.xmlを作成するには、\のようなそれらのボタンで同じであるすべてのattriburesでフォルダ値:描画可能/ main_button_states @

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

セレクタは次のようになります。

<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

しかし、すべてのセレクタを個別に作成する必要があります。すべてのセレクタコードを1つのファイルに書き込む方法もあれば、それらを再利用する方法もあります。 –

+0

@DileepPerla、各セレクターは別ファイルです。ファイル名で識別します(_ @ drawable/main_button_states_は_drawable_フォルダの_main_button_states.xml_です) – infero

+0

私はそれを入手しました。セレクタxmlを呼び出すことは私にとっては問題ではありません。セレクタxmlファイルの数を最適化したかっただけです。 –

関連する問題