2016-07-04 7 views
0

私は自分のアプリケーションにDrawerLayoutを実装しました。ステータスがステータスであるときにアイテムのイメージを変更することができたら欲しいです。こんにちは、引き出しのレイアウトの項目のテンプレートを表す次のコードがあります。ステータスが押されたときにアイテム全体の色を変更する方法はわかっていますが、イメージがLinearLayoutの子であるためイメージを変更する方法がわかりません。誰かが私を助けたり、私にそれをする方法を提案できますか?DrawerLayout item change image

おかげ

コード:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/CustomSelector" 
    android:orientation="horizontal" 
    android:minHeight="65dp"> 
    <TextView 
     android:layout_width="3dp" 
     android:layout_height="match_parent" 
     android:background="#5AC834" 
     android:layout_marginTop="1dp" 
     android:layout_marginBottom="1dp" /> 
    <ImageView 
     android:id="@+id/listImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/icon" 
     android:layout_weight="0" 
     android:layout_gravity="center_vertical" 
     android:paddingLeft="5dip" /> 
    <TextView 
     android:id="@+id/listText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#334370" 
     android:textSize="22dip" 
     android:textStyle="bold" 
     android:layout_weight="1" 
     android:layout_gravity="center_vertical" 
     android:paddingLeft="10dip" /> 
</LinearLayout> 

答えて

0

あなたはこれを達成するために、セレクタを使用することができます。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/CustomSelector" 
    android:orientation="horizontal" 
    android:minHeight="65dp"> 
    <TextView 
     android:layout_width="3dp" 
     android:layout_height="match_parent" 
     android:background="#5AC834" 
     android:layout_marginTop="1dp" 
     android:layout_marginBottom="1dp" /> 
    <ImageView 
     android:id="@+id/listImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/icon_selector" 
     android:layout_weight="0" 
     android:layout_gravity="center_vertical" 
     android:paddingLeft="5dip" /> 
    <TextView 
     android:id="@+id/listText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#334370" 
     android:textSize="22dip" 
     android:textStyle="bold" 
     android:layout_weight="1" 
     android:layout_gravity="center_vertical" 
     android:paddingLeft="10dip" /> 
</LinearLayout> 

のres /描画可能なフォルダの下にicon_selector.xmlを作成します。

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

<item android:drawable="@drawable/btn_default_disabled_focused" android:state_focused="true" android:state_enabled="false"/> 
<item android:drawable="@drawable/btn_default_focused" android:state_focused="true" android:state_enabled="true"/> 
<item android:drawable="@drawable/btn_default_disabled" android:state_enabled="false"/> 
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true"/> 
<item android:drawable="@drawable/btn_default_normal" /> 

</selector> 

プログラマブルにセレクタを変更したい場合は、以下のように変更できます。

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

こんにちは。これは単なるアイテムであれば解決できるかもしれませんが、これはDrawerLayoutのテンプレートであり、すべてのアイテムに私のカスタムアダプタからプログラムで設定した別のイメージがあります。このようには動作しません。ありがとう – CalinCosmin

+0

@CalinCosmin、私はプログラマチックにセレクタイメージを変更する私の答えを更新しました –

関連する問題