2017-01-05 2 views
0

Я имеющий linear layout, в котором он имеет один imageview и один textview, когда я коснуться linear layout, следует выделить изображение, а также текст внутри линейной схемы с различными цвет. Я попытался использовать заданный фон для линейного макета, когда он нажат, так как я использовал ontouch-listener, он не показывает цвет, даже я попытался изменить цвета программно, но это не сработало.Как выделить выбранный элемент в линейной компоновки в андроида

Может ли кто-нибудь сказать мне, как я могу изменить цвет imageview и textview при касании линейного макета.

Xml: 



    <LinearLayout 
     android:id="@+id/linear_otherexpense" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:background="@drawable/linearlayout_check" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:weightSum="1"> 

     <ImageView 
      android:id="@+id/img_otherexpense" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.7" 
      android:scaleType="centerInside" 
      android:src="@mipmap/expense" 
      android:tint="#6666FF" /> 

     <TextView 
      android:id="@+id/tv_otherexpense" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.3" 
      android:gravity="center" 
      android:text="@string/dayexpense" 
      android:tint="@color/colorPrimary" /> 
    </LinearLayout> 

Selector:

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

Код:

li_otherexpense.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      startActivity(new Intent(NewDaybook_Activity.this,  AddingNewExpense.class)); 
      NewDaybook_Activity.this.finish(); 
      return false; 
     } 
    }); 

ответ

0

Вы можете использовать этот код. это работает хорошо для меня, будь то в письменном виде анкеты будет ПОЛЕЗНЫЕ

findViewById(R.id.linear_otherexpense).setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       switch (motionEvent.getAction()) { 
        case MotionEvent.ACTION_DOWN: 

         // When you Touch Down 
         // U can change Text and Image As per your Functionality 

         break; 
        case MotionEvent.ACTION_UP: 

         //When you Release the touch 
         // U can change Text and Image As per your Functionality 

         break; 
       } 


       return true; 
      } 
     }); 
0

Попробуйте это,

linearLayout = (LinearLayout) findViewById(R.id.linear_otherexpense); 
     imageView=(ImageView)findViewById(R.id.img_otherexpense); 
     linearLayout.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       linearLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent)); 
       imageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark)); 
       return true; 
      } 
     });