2010-11-24 3 views
9

私は2つの状態(選択済みと選択解除)のボタンを持っています。ボタンの画像は状態によって異なります。どちらを使うべきですか?どのようにイメージと状態を設定するのですか?提案をしてください(私はアンドロイドに新しいです)。ImageButtonまたはボタンは何を使用しますか?

答えて

14

ドロアブルフォルダ内でxml構成を使用します。代わりに、ボタンの背景として画像を参照するのには、このXML設定(ファイル名)を参照:

例:layout.xmlでmy_button.xml

<selector 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 
    android:state_focused="true" 
    android:state_pressed="false" 
    android:drawable="@drawable/button_style1_active" /> 
<item 
    android:state_focused="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/button_style1_down" /> 
<item 
    android:state_focused="false" 
    android:state_pressed="true" 
    android:drawable="@drawable/button_style1_down" /> 
<item 
    android:drawable="@drawable/button_style1_up" /> 

</selector> 

用途:

<Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tap me" 
     android:background="@drawable/my_button"/> 

この設定では、押したときやフォーカスしたときなど、ボタンの外観に影響を与えることができます。両方のタイプのボタン(ButtonとImageButton)で同じ方法です。ボタンにテキストが含まれていない場合は、ImageButtonを使用します。

+0

1000+これは正常に動作しました。 – xydev

関連する問題