|
在我們安卓佈局使用佈局文件可以對各個控件的位置和屬性進行設置,但是在佈局一個複雜的界面,這樣使得界面比較繁雜不明朗清晰,所以可以使用一個特殊的標籤來實現特殊的功能,那就是include標籤了,顧名思義包含標籤,它可以使幾個不同的佈局文件整合在一起。
具體如下:
主佈局:include_main
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <include
- android:id="@+id/child1"
- android:layout_height="416dp"
- layout="@layout/include_child1" />
- <include
- android:id="@+id/child2"
- layout="@layout/include_child2"
- />
- </LinearLayout>
複製代碼
子布局1:child1
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:text="include子布局1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
複製代碼
子布局2:child2
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:text="include子布局2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
複製代碼
這樣佈局程序可讀性大大提高,模塊清晰明朗。
2.自定義控件效果在res文件下創建(folder)文件-文件名 為drawable,然後再其文件下創建xml文件,其下面可供選擇的有shape形狀、gradient漸變、圓角corners、Stroke描邊、solid實心、rotate旋轉、selector選擇器等等
常用shape selector如下
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
-
- <corners android:radius="5.0dip"></corners>
- <padding android:right="5dip" android:bottom="5dip"></padding>
- <solid android:color="#1d953f"></solid>
- <stroke android:width="5.0dip" android:color="#1d9530"></stroke>
- </shape>
複製代碼
根據自己的需求來設置參數,其中還可以設置其形狀android :shape="oval"oval為橢圓形,ring環形,rectangle矩形,直線型line
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:state_pressed="false" android:drawable="@drawable/normal"></item>
- <item android:state_focused="false" android:drawable="@drawable/normal"></item>
- <item android:state_pressed="true" android:drawable="@drawable/press"></item>
- <item android:state_focused="true" android:drawable="@drawable/focused"></item>
- </selector>
複製代碼
android:state_focused這個為獲取焦點事件,android:state_pressed為按下事件可以自己定義其獲取焦點前後和按下按鍵前後效果,其中@drawable/可添加圖片和自定義的drawable佈局 最後在控件引用的時候:android:background="@drawable/你自定義文件"注意在chechbox等一些控件自定義時要使用android:button="@drawable/你自定義文件",或者在style文件中設置
- <style name="logincheckbox" parent="@android:style/Widget.CompoundButton.CheckBox">
- <item name="android:button">@drawable/logincheckbox</item>
- </style>
複製代碼
控件引用的時候用style="@style/style名字"
3.巧妙的使用style來定義效果
- <style name="CustomButton" parent="@android:style/Widget.Button">
- <!-- 水平、垂直居中 -->
- <item name="android:gravity">center_vertical|center_horizontal</item>
- <!--字體顏色-->
- <item name="android:textColor">#FFFFFFFF</item>
- <!--指定文本陰影的顏色,需要與shadowRadius一起使用-->
- <item name="android:shadowColor">#FF000000</item>
- <!--設置陰影橫向坐標開始的位置-->
- <item name="android:shadowDx">0</item>
- <!--設置陰影縱向坐標開始的位置-->
- <item name="android:shadowDy">-1</item>
- <!--設置陰影的半徑,設置為0.1就變成文本的顏色了-->
- <item name="android:shadowRadius">0.2</item>
- <item name="android:textSize">16sp</item>
- <item name="android:textStyle">bold</item>
- <!--這裡引用定義的按鈕樣式-->
- <item name="android:background">@drawable/custom_button</item>
- <item name="android:focusable">true</item>
- <item name="android:clickable">true</item>
- </style>
複製代碼
根據自己的需要靈活巧妙的來設置其屬性來滿足需求
4.巧妙地使用動畫效果
5.使用九妹(.9patch )無失真圖片拉伸處理 |
|