2016-07-04 4 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/drawable 폴더 아래에 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, 나는 프로그램 방식으로 선택기 이미지를 변경하려면 내 대답을 업데이 트했습니다 –