2013-04-12 2 views
7

J'ai une disposition linéaire dans mon fichier XML et j'ajoute une autre disposition linéaire (contenant deux textviews) à cette mise en page linéaire via java. Les événements tactiles fonctionnent parfaitement, mais je veux mettre en évidence la disposition linéaire sélectionnée en définissant la couleur d'arrière-plan. Veuillez nous conseillerModification de la couleur d'arrière-plan de la mise en page linéaire en cliquant sur

+3

afficher le code ce que vous avez essayé –

+0

Implémentez les événements Touch ou Click pour 'LinearLayouts' et modifiez sa couleur d'arrière-plan en fonction de la sélection des mises en page. – GrIsHu

+0

Raghunandan a une bonne réponse - vous devez le choisir – AndrewS

Répondre

1

mettre fichier selector.xml dans le dossier drawable (res/drawable)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 


    <item android:drawable="@drawable/list_focused" android:state_pressed="true"/> 
    <item android:drawable="@drawable/list_focused" android:state_enabled="true" android:state_focused="true" android:state_window_focused="true"/> 
    <item android:drawable="@drawable/list_raw_img"/> 

</selector> 

et fond ensemble de votre mise en page linéaire dans le fichier xml

android:background="@drawable/background" 
+0

incorrect, vous ne pouvez pas nommer le fichier selector.xml puis appelez @ drawable/fond et s'attendre à ce que cela fonctionne –

45

Define background.xml dans le dossier drawable

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@drawable/pressed" /> 
<item android:state_focused="false" 
    android:drawable="@drawable/normal" /> 
</selector> 

normal.xml dans le dossier drawable

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>  
</shape> 

pressed.xml dans le dossier drawable

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>  
</shape> 

ensuite mis fond à votre mise en page

android:background="@drawable/background" 

Vous pouvez également définir l'arrière-plan comme ci-dessous

Sur tactile de votre mise en page

ll.setOnTouchListener(new View.OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      switch(event.getAction()) 
      { 
     case MotionEvent.ACTION_DOWN: 
      ll.setBackgroundColor(Color.RED); 
      break; 
      case MotionEvent.ACTION_UP: 

      //set color back to default 
      ll.setBackgroundColor(Color.WHITE); 
      break; 
      } 
      return true;   
     } 
    }); 
12

Quelle que soit la méthode que vous avez choisi (XML/code) - assurez-vous de faire votre LinearLayout cliquable:

XML:

android:clickable="true" 

code:

myLinearLayout.setClickable(true); 
+0

Merci! Son travail pour moi –

1

La méthode suivante vous voulez que vous voulez obtenir. Même l'animation d'ondulation dans Lollipop. Il suffit de passer un contexte, la vue (peut être une disposition linéaire):

public static void setClickableAnimation(final Context context, final View view) { 
    final int[] attrs = new int[]{R.attr.selectableItemBackground}; 
    final TypedArray typedArray = context.obtainStyledAttributes(attrs); 
    final int backgroundResource = typedArray.getResourceId(0, 0); 
    view.setBackgroundResource(backgroundResource); 
    typedArray.recycle(); 
} 
1

Voici ma solution, il est très simple:

<LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@drawable/bg_pressed_tab" 
      android:clickable="true" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:gravity="center" 
      android:orientation="vertical"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:contentDescription="@string/app_name" 
       android:src="@mipmap/ic_launcher" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/service_request" 
       android:textColor="@color/white" 
       android:textSize="@dimen/text_size_small" /> 

      <requestFocus /> 
     </LinearLayout> 

bg_tab_pressed.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_longAnimTime"> 

    <item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="true" /> 

    <item android:drawable="@color/colorPrimaryDark" android:state_focused="false" android:state_pressed="true" /> 

    <item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="false" /> 

    <item android:drawable="@color/colorPrimary" android:state_focused="false" android:state_pressed="false" /> 

</selector> 
Questions connexes