2016-06-21 6 views
1

ImageButtonの多くの例がホバー上にありますが、画像が変更されています。私はちょうど背景色を変更したい。プレス上でImageButtonのバックグラウンドカラーを変更する

これは私の選択である -

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

<item android:state_pressed="true" android:color="#4B88F7"/> 
<item android:state_pressed="false" android:color="#121212"/> 

そして、私のImageButton -

<ImageButton 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:background="@color/actionbar_btn" 
     android:src="@drawable/ic_remove" /> 

答えて

1

次の内容のres/drawable/ディレクトリ内のファイルcustom_selector.xml作成します。これを適用するためにその後

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@color/btn_pressed"/> 
    <item android:state_pressed="false" android:drawable="@color/btn_released"/> 
</selector> 

をsettiのImageButtonへの背景backgroundとしてそれをngの:

<ImageButton 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    android:background="@drawable/custom_selector.xml" 
    android:src="@drawable/ic_remove" /> 

は、それが例外を与えたhttps://developer.android.com/guide/topics/resources/color-list-resource.html

+0

を参照してください。タグは描画可能な属性または子タグを必要とします。 – reza

+0

@reza、私は私の投稿を編集しました。色は 'btn_pressed'と' btn_released'を作成する必要があります。 – Kairat

+0

ありがとうございました。 – reza

関連する問題