2012-02-29 10 views
1

状態に基づいた背景画像(この画像は9パッチではありません)とテキストを含むXMLレイアウト(可能な場合)で「ボタン」を作成します。どのコンポーネントを使用しますか?画像の背景とテキストとステートフル機能を備えたAndroidボタン

マイbutton_states.xmlは、次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/my_pressed" /> 
    <item android:drawable="@drawable/my_normal" /> 
</selector> 

my_pressedmy_normalNON 9 - パッチを適用した画像です。

今では、背景画像なしでレンダリングされ、私は通常のボタンを使用する場合

<Button 
    android:id="@+id/my_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_states" 
    android:text="@string/my_text" /> 

ボタン。

テキストとステートフルな「静的」背景を持つ「ボタン」の操作方法は?

Thxを

よろしく

答えて

0

これを実現する最も簡単な方法は、コードでそれをやっています。 (通常)ボタンにsetBackgroundDrawable()を使用して、目的の背景を設定します。あなたのセレクタを提供されたドロアブルとして使用するなら、あなたは大丈夫でしょう。

[編集]は、この使用してリソースを行うの場所にあなたのres \次のコード使用中の描画可能なフォルダにbutton_state.xml

Button button = (Button) findViewById(R.id.my_button); 
button.setBackgroundResource(R.drawable.button_state); 
+0

Thxを...私は...それは可能ですみますセレクタでsetBackgroundDrawable()を使用するには?はいの場合、どのように? – zmeda

関連する問題