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

相关文章
相关标签/搜索