Android LinearLayout 點擊背景顏色改變

爲了更好地用戶體驗, 當用戶點擊的時候, 要給用戶feedback, 在一個佈局中, 點擊的時候背景顏色改變, 但咱們有時候又沒有專門的設計人員,這時候就本身了.以下圖:java

點擊的時候顏色會改變android

佈局的代碼以下佈局

<!-- 簽名 -->
         <LinearLayout 
                android:orientation="horizontal" 
                //這裏的background下面會說到
                android:background="@drawable/layout_white_background" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:clickable="true"
                >
                <TextView 
                    android:textSize="16.0sp" 
                    android:textColor="@color/text_light" 
                    android:gravity="center" 
                    android:layout_gravity="top" 
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" 
                    android:layout_marginLeft="10.0dip" 
                    android:layout_marginTop="10.0dip" 
                    android:text="籤 名" />
                <TextView 
                    android:textColor="@color/text_light" 
                    android:gravity="left" 
                    android:layout_width="0.0dip" 
                    android:layout_height="wrap_content" 
                    android:layout_marginLeft="3.0dip" 
                    android:layout_weight="1.0" />
                <FrameLayout 
                    android:id="@+id/layout_description_more" 
                    android:paddingLeft="5.0dip" 
                    android:layout_width="30.0dip" 
                    android:layout_height="45.0dip">
                    <ImageView
                        android:layout_gravity="left|center" 
                        android:layout_width="10.0dip" 
                        android:layout_height="10.0dip" 
                        android:src="@drawable/arrow" 
                        />
                </FrameLayout>
         </LinearLayout>

上面中最主要的就是LinearLayout的background, 這裏說了點擊的時候的背景是什麼設計

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/lilayout_dark" />
    <item android:state_pressed="true" android:drawable="@color/layout_dark" />
    <item android:drawable="@color/layout_light" />
</selector>
 //上面的
//color/lilayout_dark是在colors.xml中定義了的
<color name="layout_dark">#fff4f4f4</color>
//color/layout_light
<color name="layout_light">#ffffffff</color>

OK 搞定, 這樣點擊的時候就會背景不同了code

相關文章
相關標籤/搜索