2013-04-12 18 views
7

Mam układ liniowy w moim xml i dodaję kolejny układ liniowy (zawierający dwa widoki tekstowe) do tego układu liniowego za pośrednictwem java. Wydarzenia dotykowe działają idealnie, ale chcę zaznaczyć wybrany układ liniowy, ustawiając kolor tła. Proszę o poradę.Zmiana koloru tła Układ liniowy podczas klikania

+3

pisać kod co wypróbowałeś –

+0

Zaimplementuj zdarzenia dotyku lub kliknięcia dla "LinearLayouts" i zmień kolor tła zgodnie z wyborem układów. – GrIsHu

+0

Raghunandan ma świetną odpowiedź - powinieneś go wybrać: – AndrewS

Odpowiedz

1

put plik selector.xml w rozciągliwej folderu (res/rozciągliwej)

<?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> 

i ustawić tło układu liniowego w pliku xml

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

niepoprawny, nie możesz nazwać pliku selector.xml, a następnie wywołać @ drawable/background i oczekiwać, że zadziała –

45

Definiowanie background.xml w rozciągliwej folderze

<?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 w rozciągliwej folderze

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

pressed.xml w rozciągliwej folderze

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

następnie ustaw tło do twojego Układ

android:background="@drawable/background" 

Można również ustawić tło jak poniżej

Na Dotyk układu

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

Niezależnie od metody wybranej (XML/code) - upewnij się dokonać LinearLayout klikalne:

XML:

android:clickable="true" 

Kod:

myLinearLayout.setClickable(true); 
+0

Dzięki! Jego praca dla mnie –

1

Poniższa metoda dostanie chcesz chcesz. Nawet animacja marszczyć w Lollipop. Wystarczy zdać kontekście pogląd (może być układ liniowy):

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

Oto moje rozwiązanie, to jest bardzo proste:

<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>