Android中UI設計的優化

jopen 10年前發布 | 28K 次閱讀 Android Android開發 移動開發
1.使用安卓include標簽
   在我們安卓布局使用布局文件可以對各個控件的位置和屬性進行設置,但是在布局一個復雜的界面,這樣使得界面比較繁雜不明朗清晰,所以可以使用一個特殊的標簽來實現特殊的功能,那就是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:include_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:include_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)無失真圖片拉伸處理
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!