2014-10-09 2 views
4

: state_pressed = "true"로 안드로이드 : 목록 내 행 (row_item.xml) 나는 안드로이드 변경할누른 색상을 다른 색상의 선택기로 동적으로 변경하는 방법은 무엇입니까? 목록은 내 선택이야

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/alarm_item_drawer_selector" 
    > 

    <ImageView 
     android:id="@+id/item_icon" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_centerHorizontal="true" 
     android:paddingTop="5dp" 
     android:paddingBottom="2dp" 
     android:src="@drawable/ic_launcher"/> 

    <TextView  
     android:id="@+id/item_title" 
     style="@style/DrawerItemTextStyle" 
     android:textColor="@color/text_drawer_item_selector" 
     android:layout_centerHorizontal="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/item_icon" /> 

</RelativeLayout> 

있어 (item_selector.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:drawable="@color/orange" /> 
    <item android:state_focused="true" android:drawable="@color/orange" /> 
    <item android:state_pressed="true" android:drawable="@color/orange" /> 
    <item android:drawable="@color/transparent" /> 
</selector> 

: drawable = "@ color/orange"를 다른 색상에 동적으로 적용합니다. 내 목표는, 각 행마다 다른 색깔을 적용하는 것입니다. 할 수 있습니까?

+1

는 당신이 항목을 클릭하십시오'setOnItemClickListener'을해야 할 수도 있습니다 그 코드 내에서 내가의 getView 내부 색상을 변경하는 것을 선호 선택 – Saqib

+0

의 항목 내가 뭔가 보이는 것 같아요의 색상으로 변경할 수 있습니다보기 의 1. GET 선택을 2. 눌러 진 상태의 색을 변경하십시오. 3. 다시 적용하십시오 – user1711993

답변

7

새 StateListDrawable에 영향을줍니다.

StateListDrawable stateListDrawable= new StateListDrawable(); 
    stateListDrawable.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor))); 
    stateListDrawable.addState(new int[] {android.R.attr.state_focused}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor))); 
    stateListDrawable.addState(new int[] {android.R.attr.state_selected}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor))); 
    stateListDrawable.addState(new int[] {}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor))); 

    view.setBackgroundDrawable(stateListDrawable); 
0

동적으로 변경하려면 코드로 수행해야합니다. 이를 위해서는 onTouchListener를 사용해야합니다.보기에서 터치하거나 놓을 때를 알고 싶기 때문입니다.

final TextView textView =findViewById(R.id.item_title); 
    textView.setOnTouchListener(new View.OnTouchListener() 
    { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) 
     { 
      switch (motionEvent.getAction()){ 
       case MotionEvent.ACTION_DOWN: 
        textView.setTextColor(Color.RED); 
        break; 
       case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL: 
        textView.setTextColor(Color.BLACK); 
        break; 
      } 
      return false; 
     } 
    }); 

위의 코드는 텍스트 뷰의 텍스트 색을 변경합니다. Color.RED을 원하는 색으로 변경하십시오.

+0

감사합니다 Pedro! 하지만 질문이 있습니다 이런 식으로 할 수 있습니까? 1. 뷰 2 선택기 가져 오기 상태의 색상 변경 3. 다시 적용 – user1711993

+0

클릭 수신기에서 색상이 동적으로 변경된다고 생각합니다. 올바르지 않은 해결책입니다. ( 나는 배울 수있는 안드로이드 개발 및 내 목표에 익숙합니다. 맞음 – user1711993

+0

버튼을 놓을 때 말하지 않기 때문에 청취자가 클릭 할 수 없게됩니다. –

1

내가 프로젝트에서 수행 한 작업은 어떤보기를 취해서 내가 만든 여섯 가지 색상 선택기 중 하나로 배경을 변경하는 정적 방법을 만들었습니다. 여기의 트릭은 listview selector를 변경하려고하지 않고 listview 레이아웃의 부모 레이아웃의 배경을 변경하려고합니다.

listview.xml (리스트 뷰의 실제 내용) 내 어댑터

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/parent" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@android:color/white" 
    android:id="@+id/text" 
    android:text="Text" 
    android:padding="20dp"/> 

</LinearLayout> 

의 getView() 메소드 : 변경

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 

    Holder holder = new Holder(); 
    if (view == null) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     view = inflater.inflate(R.layout.listview, viewGroup, false); 
    } else { 

     holder = (Holder) view.getTag(); 
    } 

    holder.parent = (LinearLayout) view.findViewById(R.id.parent); 
    setRandomListViewSelector(holder.parent); 
    holder.text = (TextView) view.findViewById(R.id.text); 
    holder.text.setText(strs.get(i)); 

    return view; 
} 

정적 메소드 여기

는 예제 배경 :

public static void setRandomListViewSelector(View parentView) { 

     int i = new Random().nextInt(2); 

     switch (i) { 
      case 0: 
       parentView.setBackgroundResource(R.drawable.list_selector_blue); 
       break; 

      case 1: 
       parentView.setBackgroundResource(R.drawable.list_selector_red); 
       break; 
     } 

} 

list_selector_blue.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true"> 
    <shape> 
     <solid android:color="@android:color/holo_blue_dark"/> 
    </shape> 
</item> 
</selector> 

더 많은 제어를 원하는 경우 내 예제에서, 난 그냥 무작위로 0과 1 사이의 숫자를 생성하는 임의의 클래스를 사용하면 전역 변수와 증가로를 만들 수 있습니다 1을 사용하면 배경색을 설정하는 방법을보다 잘 제어 할 수 있습니다.