天天看點

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

寫完部落格的總結 : 以前沒有弄清楚的概念清晰化

父容器與本容器屬性 : android_layout...屬性是本容器的屬性, 定義在這個布局管理器的LayoutParams内部類中, 每個布局管理器都有一個LayoutParams内部類, android:... 是父容器用來控制子元件的屬性. 如android:layout_gravity 是控制元件本身的對齊方式, android:gravity是控制本容器子元件的對齊方式;

.

作者 :萬境絕塵 

轉載請注明出處 : http://blog.csdn.net/shulianghan/article/details/18964835

.

布局管理器都是以ViewGroup為基類派生出來的; 使用布局管理器可以适配不同手機螢幕的分辨率,尺寸大小;

布局管理器之間的繼承關系 : 

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

在上面的UML圖中可以看出, 絕對布局 幀布局 網格布局 相對布局 線性布局是直接繼承ViewGroup,表格布局是繼承的LinearLayout;

一. 線性布局(LinearLayout)

1. 線性布局作用 

作用 : 線性布局會将容器中的元件一個一個排列起來, LinearLayout可以控制元件 橫向 或者 縱向 排列, 通過android:orientation屬性控制;

不換行屬性 : 線性布局中的元件不會自動換行, 如果元件一個一個排列到盡頭之後, 剩下的元件就不會顯示出來;

2. LinearLayout常用屬性

(1)基線對齊

xml屬性 : android:baselineAligned; 

設定方法 : setBaselineAligned(boolean b); 

作用 : 如果該屬性為false, 就會阻止該布局管理器與其子元素的基線對齊;

(2)設分隔條 

xml屬性 : android:divider; 

設定方法 : setDividerDrawable(Drawable); 

作用 : 設定垂直布局時兩個按鈕之間的分隔條;

(3)對齊方式(控制内部子元素)  

xml屬性 : android:gravity; 

設定方法 : setGravity(int); 

作用 : 設定布局管理器内元件(子元素)的對齊方式, 

支援的屬性 : 

top, bottom, left, right, 

center_vertical(垂直方向居中), center_horizontal(水準方向居中),

fill_vertical(垂直方向拉伸), fill_horizontal(水準方向拉伸), 

center, fill, 

clip_vertical, clip_horizontal; 

可以同時指定多種對齊方式 : 如 left|center_vertical 左側垂直居中;

(4)權重最小尺寸 

xml屬性 : android:measureWithLargestChild; 

設定方法 : setMeasureWithLargestChildEnable(boolean b);

作用 : 該屬性為true的時候, 所有帶權重的子元素都會具有最大子元素的最小尺寸;

(5) 排列方式

xml屬性 : android:orientation;

設定方法 : setOrientation(int i);

作用 : 設定布局管理器内元件排列方式, 設定為horizontal(水準),vertical(垂直), 預設為垂直排列;

3. LinearLayout子元素控制

LinearLayout的子元素, 即LinearLayout中的元件, 都受到LinearLayout.LayoutParams控制, 是以LinearLayout包含的子元素可以執行下面的屬性.

(1) 對齊方式

xml屬性 : android:layout_gravity;

作用 : 指定該元素在LinearLayout(父容器)的對齊方式, 也就是該元件本身的對齊方式, 注意要與android:gravity區分, ;

(2) 所占權重

xml屬性 : android:layout_weight;

作用 : 指定該元素在LinearLayout(父容器)中所占的權重, 例如都是1的情況下, 那個方向(LinearLayout的orientation方向)長度都是一樣的;

4. 控制子元素排列 與 在父元素中排列

控制本身元素屬性與子元素屬性 : 

裝置元件本身屬性 : 帶layout的屬性是設定本身元件屬性, 例如 android:layout_gravity設定的是本身的對其方式; 

設定子元素屬性 : 不帶layout的屬性是設定其所包含的子元素, 例如android:gravity 設定的是該容器子元件的對齊方式;

LayoutParams屬性 : 所有的布局管理器都提供了相應的LayoutParams内部類, 這些内部類用于控制該布局本身, 如 對齊方式 layout_gravity, 所占權重 layout_weight, 這些屬性用于設定本元素在父容器中的對齊方式;

容器屬性 : 在android:後面沒有layout的屬性基本都是容器屬性, android:gravity作用是指定指定本元素包含的子元素的對齊方式, 隻有容器才支援這個屬性;

5. 常見用法

(1) 擷取LinearLayout的寬高

a. 元件外無法擷取元件寬高 

下面的兩種情況都是針對 View.getHeight() 和 View.getWidth() 方法 : 

元件外無法擷取 : 調用View.getHeight() 和View.getWidth()方法 是擷取不到元件的寬度和高度的, 這兩個方法傳回的是0, Android的運作機制決定了無法在元件外部使用getHeight()和getWidth()方法擷取寬度和高度;

元件内可以擷取 : 在自定義的類中可以在View的類中通過調用這兩個方法擷取該View子類元件的寬和高;

b. 元件外部擷取View對象寬高方法 

外部擷取 : 使用View.getMeasuredWidth() 和View.getMeasuredHeight()方法可以擷取元件的寬和高, 在調用這個方法之前, 必須先調用View.measure()方法, 才可以, 否則也擷取不到元件的寬高;

注意(特例) : 如果元件寬度或高度設定為 fill_parent, 使用 getMeasuredHeight() 等方法擷取寬度和高度的時候, 并且元件中含有子元素時, 所擷取的實際值是這些元件所占的最小寬度和最小高度.(沒看懂)

示例:

View view = getLayoutInflater().inflate(R.layout.main, null);
		LinearLayout layout = (LinearLayout) view.findViewById(R.id.linearlayout);
		//調用測量方法, 調用了該方法之後才能通過getMeasuredHeight()等方法擷取寬高
		layout.measure(0, 0);
		//擷取寬度
		int width = layout.getMeasuredWidth();
		//擷取高度
		int height = layout.getMeasuredHeight();
           

c. 擷取布局檔案中元件的寬高 

從LayoutParams中擷取 : 調用View.getLayoutParams().width 和 View.getLayoutParams().height 擷取寬高, 如果寬高被設定為 fill_parent, match_parent, warp_content 時, 這兩個兩邊直接回傳回 FILL_PARENT, MATCH_PARENT, WARP_CONTENT常量值;

規律 : 從View.getLayoutParams()中擷取 width, height 值, 在布局xml檔案中設定的是什麼, 擷取的時候就得到的是什麼;

(2) 在LinearLayout中添加分隔線

a. 使用ImageView添加(低版本3.0以下)

垂直布局 橫向寬度填滿 : 如果布局是vertical, 那麼設定一個ImageView寬度fill_parent, 高度2dp, 設定一個背景色;

水準布局 縱向高度填滿 : 如果布局時horizontal, 那麼設定一個ImageView寬度2dp, 高度fill_parent, 設定一個背景色;

<ImageView 
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:background="#F00"/>
           

b. 使用xml屬性添加(3.0以上版本)

設定LinearLayout标簽的 android:showDividers屬性, 該屬性有四個值 : 

none :不顯示分隔線;

beginning : 在LinearLayout開始處顯示分隔線;

middle : 在LinearLayout中每兩個元件之間顯示分隔線;

end : 在LinearLayout結尾處顯示分隔線;

設定android:divider屬性, 這個屬性的值是一個Drawable的id;

c. 使用代碼添加(3.0以上版本)

設定顯示分隔線樣式 : linearLayout.setShowDividers(), 設定android:showDividers屬性;

設定分隔線圖檔 : linearLayout.setDividerDrawable(), 設定android:divider屬性;

6. 實際案例

(1) 按鈕排列 

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高
【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

要點 : 

底部 + 水準居中 對齊屬性 : 左邊的LinearLayout的android:gravity 屬性為bottom|center_horizontal; 

右部 + 垂直居中 對齊屬性 : 右邊的LinearLayout的android:gravity 屬性為right|center_vertical;

代碼 : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:gravity="bottom|center_horizontal">
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕1"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="測試按鈕2"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕3"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="測試按鈕4"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕5"/>
</LinearLayout>
           

子元素對齊 : 通過修改 android:gravity 屬性來控制LinearLayout中子元素的排列情況;

左邊的圖的屬性為 bottom|center_horizontal , 右邊的android:gravity的屬性值為 right|center_vertical;

(2) 三個按鈕各自對齊

三個水準方向的按鈕, 分别左對齊, 居中對齊, 右對齊 :

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

要點 : 

水準線性布局 : 最頂層的LinearLayout的orientation是horizontal水準的;

等分三個線性布局 : 第二層的LinearLayout的orientation是vertical垂直的, 并且寬度是fill_parent , 依靠權重配置設定寬度;

設定按鈕對齊方式 : 按鈕的android:layout_gravity屬性根據需求 left, center, right, 預設為left;

代碼 : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"  >
   
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#f00">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕1"/>
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#0f0">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕2"
            android:layout_gravity="center"/>
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#00f">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕3"
            android:layout_gravity="right"/>
    </LinearLayout>
    
</LinearLayout>
           

二. 相對布局RelativeLayout

相對布局容器中, 子元件的位置總是相對兄弟元件,父容器來決定的;

1. RelativeLayout支援的屬性

(1) 對齊方式

xml屬性 : android:gravity;

設定方法 : setGravity(int);

作用 : 設定布局容器内子元素的對齊方式, 注意與android:layout_gravity區分, 後者是設定元件本身元素對齊方式;

(2) 忽略對齊方式

xml屬性 : android:ignoreGravity;

設定方法 : setIgnoreGravity(int);

作用 : 設定該元件不受gravity屬性影響, 因為gravity屬性影響容器内所有的元件的對齊方式, 設定了之後, 該元件就可以例外;

2. LayoutParams屬性

(1) 隻能設定boolean值的屬性

這些屬性都是相對父容器的, 确定是否在父容器中居中(水準, 垂直), 是否位于父容器的 上下左右 端;

是否水準居中 : android:layout_centerHorizontal;

是否垂直居中 : android:layout_centerVertical;

是否位于中央 : android:layout_centerInParent;

是否底端對齊 : android:layout_alignParentBottom;

是否頂端對齊 : android:layout_alignParentTop;

是否左邊對齊 : android:layout_alignParentLeft;

是否右邊對齊 : android:layout_alignParentRight;

(2) 隻能設定其它元件id的屬性

位于所給id元件左側 : android:layout_toLeftOf;

位于所給id元件右側 : android:layout_toRightOf;

位于所給id元件的上邊 : android:layout_above;

位于所給id元件的下方 : android:layout_below;

與所給id元件頂部對齊 : android:layout_alignTop;

與所給id元件底部對齊 : android:layout_alignBottom;

與所給id元件左邊對齊 : android:layout_alignLeft;

與所給id元件右邊對齊 : android:layout_alignRight;

3. 梅花布局效果 

五個按鈕排成梅花形狀, 梅花處于正中心, 效果圖如下 :

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

兩個按鈕, 如果隻有 android:layout_above="@+id/bt1" 會是這種情況 : 

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

加上 android:layout_alignLeft="@+id/bt1"就會成為這種情況 : 

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

要點 : 

注意每個元件的屬性, 先要确定方位, 在進行對齊, 元件左邊界對齊, 元件上邊界對齊;

代碼 : 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <Button 
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕1"
        android:layout_centerInParent="true"/>
    
    <Button 
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕2"
        android:layout_above="@+id/bt1"
        android:layout_alignLeft="@+id/bt1"/>
    
    <Button 
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕3"
        android:layout_centerInParent="true"
        android:layout_below="@+id/bt1"
        android:layout_alignLeft="@+id/bt1"/>
    
    <Button 
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕4"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@+id/bt1"
        android:layout_alignTop="@+id/bt1"/>
    
    <Button 
        android:id="@+id/bt5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕5"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/bt1"
        android:layout_alignTop="@+id/bt1"/>
    
</RelativeLayout>
           

4. 相對布局常用方法

(1) 擷取螢幕中一個元件的位置

建立數組 : 要先建立一個整型數組, 數組大小2位; 這個數組傳入getLocationOnScreen()方法;

将位置資訊傳入數組 : 可以調用View.getLocationOnScreen()方法, 傳回的是一個數組 int[2], int[0] 是橫坐标, int[1] 是縱坐标;

//擷取元件
		Button b = (Button) this.findViewById(R.id.Button01);
		//建立數組, 将該數組傳入getLocationOnScreen()方法
		int locations[] = new int[2];
		//擷取位置資訊
		b.getLocationOnScreen(locations);
		//擷取寬度
		int width = locations[0];
		//擷取高度
		int height = locations[1];
           

(2) LayoutParams的使用設定所有屬性

屬性設定方法少 : Android SDK中View類隻提供了很少用于設定屬性的方法,大多數屬性沒有直接對應的獲得和設定屬性值的方法, 看起來貌似不是很好用;

使用LayoutParams設定屬性值 : Android中可以對任何屬性進行設定, 這裡我們需要一個LayoutParams對象, 使用這個LayoutParams.addRule()方法, 可以設定所有元件的屬性值; 設定完之後調用View.setLayoutParams()方法, 傳入剛才建立的LayoutParams對象, 并更新View的相應的LayoutParams屬性值, 向容器中添加該元件;

代碼中動态設定布局屬性 : 

a. 建立LayoutParams對象

b. 調用LayoutParams對象的addRule()方法設定對應屬性;

c. 調用View.setLayoutParams()方法将設定好的LayoutParams對象設定給元件;

d. 調用addView方法将View對象設定到布局中去;

使用代碼設定android:layout_toRightOf 和 android:layout_below屬性 : 

//裝載布局檔案
		RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.relative, null);
		//裝載要動态添加的布局檔案
		Button button = (Button) relativeLayout.findViewById(R.id.bt1);
		//建立一個LayoutParams對象
		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		//設定android:layout_toRightOf屬性
		layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.bt2);
		//設定android:layout_below
		layoutParams.addRule(RelativeLayout.BELOW, R.id.bt2);
		//更新Button按鈕的屬性
		button.setLayoutParams(layoutParams);
		//向布局中動态添加按鈕
		relativeLayout.addView(button);
           

三. 幀布局FrameLayout

幀布局容器為每個元件建立一個空白區域, 一個區域成為一幀, 這些幀會根據FrameLayout中定義的gravity屬性自動對齊;

1. 繪制霓虹燈布局

繪制一個霓虹燈效果的層疊布局, 如下圖 : 

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

要點 : 

後擋前 : 後面的View元件會遮擋前面的View元件,越在前面, 被遮擋的機率越大;

界面居中 : 将所有的TextView元件的對齊方式 android:layout_gravity 設定為center;

正方形 : 所有的TextView都設定android:height 和 android:width 屬性, 用來設定其寬高, 這裡設定成正方形, 寬高一樣, 後面的元件比前面的邊長依次少40;

顔色 : 每個TextView的背景都設定成不一樣的;

代碼 : 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TextView 
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="320px"
        android:height="320px"
        android:background="#f00"/>
    <TextView 
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="280px"
        android:height="280px"
        android:background="#0f0"/>
    <TextView 
        android:id="@+id/tv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="240px"
        android:height="240px"
        android:background="#00f"/>
    <TextView 
        android:id="@+id/tv_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="200px"
        android:height="200px"
        android:background="#ff0"/>
    <TextView 
        android:id="@+id/tv_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="160px"
        android:height="160px"
        android:background="#f0f"/>
    <TextView 
        android:id="@+id/tv_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:width="120px"
        android:height="120px"
        android:background="#0ff"/>

</FrameLayout>
           

.

作者 :萬境絕塵 

轉載請注明出處 : http://blog.csdn.net/shulianghan/article/details/18964835

.

2. 使用代碼使上面的霓虹燈效果動起來

(1) 圖檔效果 

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

(2) 顔色資源

建立顔色資源, 在跟節點<resources>下面建立<color>子節點, color屬性标簽 name 自定義, 子文本為顔色代碼;

(3) 定時器控制handler

建立Handler對象, 實作handleMessage()方法, 在這個方法中循環設定 TextView對象的顔色變量, 使用color[(i + currentColor)%colors.length]每調用一次, 就将所有的TextView顔色依次調換一次;

在onCreate()方法中, 開啟一個定時器Timer, 每隔0.2s就調用一個handler中的方法, 這樣動态的霓虹燈代碼就顯示出來了.

(4) 代碼

顔色資源代碼 : 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name = "color1">#f00</color>
    <color name = "color2">#0f0</color>
    <color name = "color3">#00f</color>
    <color name = "color4">#ff0</color>
    <color name = "color5">#f0f</color>
    <color name = "color6">#0ff</color>
</resources>
           

代碼 : 

package com.example.framelayout;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity {

	//這個變量用來控制霓虹燈顔色變化
	private int currentColor = 0;
	//顔色對應的資源id
	final int[] colors = new int[]{
			R.color.color1,
			R.color.color2,
			R.color.color3,
			R.color.color4,
			R.color.color5,
			R.color.color6
	};
	//View元件對應的資源id
	final int[] names = new int[]{
			R.id.tv_1,
			R.id.tv_2,
			R.id.tv_3,
			R.id.tv_4,
			R.id.tv_5,
			R.id.tv_6
	};
	
	//元件數組
	TextView[] views = new TextView[names.length];
	
	//定義這個Handler, 為了在定時器中固定調用handleMessage方法
	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			if(msg.what == 0x123){
				for(int i = 0; i < names.length; i ++){
					views[i].setBackgroundResource(colors[(i + currentColor) % names.length]);
				}
				currentColor ++;
			}
		};
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame);
        //初始化元件數組
        for(int i = 0; i < names.length; i ++){
        	views[i] = (TextView) findViewById(names[i]);
        }
        //每隔0.2秒更換一次顔色
        new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				handler.sendEmptyMessage(0x123);
			}
		}, 0, 200);
    }
}
           

3. 三個水準方向的按鈕分别左對齊,居中對齊,右對齊

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

要點 : 給FrameLayout中的三個按鈕分别設定 不同的layout_gravity,left ,center_horizontal,right, 就能設定成上圖的樣式;

代碼 : 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕1"
        android:layout_gravity="left"/>
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕2"
        android:layout_gravity="center_horizontal"/>
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕3"
        android:layout_gravity="right"/>

</FrameLayout>
           

四. 表格布局TableLayout

1. 表格布局的一些概念

繼承關系 : 表格布局繼承了LinearLayout, 其本質是線性布局管理器; 

控制元件 : 表格布局采用 行, 列 形式管理子元件, 但是并不需要聲明有多少 行列, 隻需要添加TableRow 和 元件 就可以控制表格的行數和列數, 這一點與網格布局有所不同, 網格布局需要指定行列數;

增加行的方法 : 

a. TableRow增加行列 : 向TableLayout中添加一個TableRow,一個TableRow就是一個表格行, 同時TableRow也是容器, 可以向其中添加子元素, 每添加一個元件, 就增加了一列;

b. 元件增加行 : 如果直接向TableLayout中添加元件, 就相當于直接添加了一行;

列寬 : TableLayout中, 列的寬度由該列最寬的單元格決定, 整個表格的寬度預設充滿父容器本身;

2. 單元格行為方式 

(1) 行為方式概念

a. 收縮 :Shrinkable, 如果某列被設為Shrinkable, 那麼該列所有單元格寬度可以被收縮, 保證表格能适應父容器的寬度;

b. 拉伸 :Stretchable, 如果某列被設為Stretchable, 那麼該列所有單元格的寬度可以被拉伸, 保證表格能完全填滿表格剩餘空間;

d. 隐藏 :Collapsed, 如果某列被設定成Collapsed, 那麼該列所有單元格會被隐藏;

(2) 行為方式屬性

a. 隐藏

xml屬性 : android:collapsedColumns;

設定方法 : setColumnCollapsed(int, boolean);

作用 : 設定需要被隐藏的列的序号, 在xml檔案中, 如果隐藏多列, 多列序号間用逗号隔開;

b. 拉伸

xml屬性 : android:stretchColumns;

設定方法 : setStretchAllColumns(boolean);

作用 : 設定允許被拉伸的列的序列号, xml檔案中多個序列号之間用逗号隔開;

c. 收縮

xml屬性 : android:shrinkableColumns;

設定方法 : setShrinkableAllColumns(boolean);

作用 : 設定允許被收縮的列的序号, xml檔案中, 多個序号之間可以用逗号隔開;

3. 表格布局執行個體

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

實作要點 : 

獨自一行按鈕 : 向TableLayout中添加按鈕, 這個按鈕就會獨自占據一行;

收縮按鈕: 在TableLayout标簽中,設定android:stretchable屬性标簽, 屬性值是要收縮的列, 注意,列标從0開始;

拉伸按鈕 : 在TableLayout标簽中,設定android:shrinkable屬性标簽, 屬性值是要拉伸的列, 注意, 清單從0開始;

代碼 : 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- LinearLayout預設是水準的, 這裡設定其方向為垂直 -->
    
    <!-- 表格布局, 第2列允許收縮, 第3列允許拉伸, 注意這裡行列的計數都是從0開始的 -->
    <TableLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="2">
        
        <!-- 向TableLayout中直接添加元件, 獨占一行 -->
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="獨自一行的按鈕"/>
        
        <TableRow>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通的按鈕"/>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="收縮的按鈕"/>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸的按鈕"/>
        </TableRow>
        
    </TableLayout>
    
    <!-- 第二個按鈕會隐藏掉 -->
    <TableLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1">
        
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="獨自一行的按鈕"/>
        
        <TableRow >
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按鈕1"/>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按鈕2"/>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按鈕3"/>
        </TableRow>
        
    </TableLayout>

    <!-- 指定第二列和第三列可以被拉伸 -->
    <TableLayout 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:stretchColumns="1,2">
        
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="獨自占一行的按鈕"/>
        
        <TableRow >
            
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按鈕1"/>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸的按鈕"/>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸的按鈕"/>
            
        </TableRow>
        
        <TableRow >
            
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通的按鈕"/>
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸的按鈕"/>
            
        </TableRow>
        
    </TableLayout>
	
    
</LinearLayout>
           

五. 網格布局

1. 網格布局介紹

網格布局時Android4.0版本才有的, 在低版本使用該布局需要導入對應支撐庫;

GridLayout将整個容器劃分成rows * columns個網格, 每個網格可以放置一個元件. 還可以設定一個元件橫跨多少列, 多少行. 不存在一個網格放多個元件情況;

2. 網格布局常用屬性

(1) 設定對齊模式

xml屬性 : android:alignmentMode;

設定方法 : setAlignmentMode(int);

作用 : 設定網格布局管理器的對齊模式

(2) 設定列數

xml屬性 : android:columnCount;

設定方法 : setColumnCount(int);

作用 : 設定該網格布局的列數;

(3) 設定是否保留列序列号

xml屬性 : android:columnOrderPreserved;

設定方法 : setColumnOrderPreserved(boolean);

作用 : 設定網格容器是否保留列序列号;

(4) 設定行數

xml屬性 : android:rowCount;

設定方法 : setRowCount(int);

作用 : 設定該網格的行數;

(5) 設定是否保留行序列号

xml屬性 : android:rowOrderPreserved;

設定方法 : setRowOrderPreserved(int);

作用 : 設定該網格容器是否保留行序列号;

(6) 頁邊距

xml屬性 : android:useDefaultMargins;

設定方法 : setUseDefaultMargins(boolean);

作用 : 設定該布局是否使用預設的頁邊距;

3. GridLayout的LayoutParams屬性

(1) 設定位置列

xml屬性 : android:layout_column;

作用 : 設定子元件在GridLayout的哪一列;

(2) 橫向跨列

xml屬性 : android:layout_columnSpan;

作用 : 設定該子元件在GridLayout中橫向跨幾列;

(3) 占據空間方式

xml屬性 : android:layout_gravity;

設定方法 : setGravity(int);

作用 : 設定該元件采用何種方式占據該網格的空間;

(4) 設定行位置

xml屬性 : android:layout_row;

作用 : 設定該子元件在GridLayout的第幾行;

(5) 設定橫跨行數

xml屬性 : android:layout_rowSpan;

作用 : 設定該子元件在GridLayout縱向橫跨幾行;

4. 實作一個計算機界面

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

(1) 布局代碼

設定行列 : 設定GridLayout的android:rowCount為6, 設定android:columnCount為4, 這個網格為 6行 * 4列 的;

設定橫跨四列 : 設定TextView和按鈕橫跨四列android:layout_columnSpan 為4, 列的合并 就是占了一行;

textView的一些設定: 

設定textView中的文本與邊框有5像素間隔 : android:padding = "5px";

代碼 : 

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:rowCount="6"
    android:columnCount="4"
    android:id="@+id/root">
    
    <!-- 
    	定義一個  6行 * 4列 GridLayout, 在裡面定義兩個元件
    	兩個元件都橫跨4列, 單獨占一行
     -->
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:textSize="50sp"
        android:layout_marginLeft="4px"
        android:layout_marginRight="4px"
        android:padding="5px"
        android:layout_gravity="right"
        android:background="#eee"
        android:textColor="#000"
        android:text="0"/>
    
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"/>

</GridLayout>
           

(2) Activity代碼

将元件設定給GridLayout網格流程 : 

指定元件所在行 : GridLayout.SpecrowSpec = GridLayout.spec(int); 

指定元件所在列 : GridLayout.SpeccolumnSpec = GridLayout.spec(int);

建立LayoutParams對象 : GridLayout.LayoutParams params =new GridLayout.LayoutParams(rowSpec, columnSpec);

指定元件占滿容器 : params.setGravity(Gravity.FILL);

将元件添加到布局中 : gridLayout.addView(view, params);

代碼 : 

package com.example.caculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridLayout.LayoutParams;
import android.widget.GridLayout.Spec;

public class MainActivity extends Activity {

	private GridLayout gridLayout;
	//需要放到按鈕上的字元串
	String chars[] = new String[]{
		"7", "8", "9", "/",
		"4", "5", "6", "*",
		"1", "2", "3", "-",
		".", "0", "=", "+"
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        gridLayout = (GridLayout) findViewById(R.id.root);
        for(int i = 0; i < chars.length; i ++){
        	Button button = new Button(this);
            button.setText(chars[i]);
            button.setTextSize(40);
            //指定元件所在行
            Spec rowSpec = GridLayout.spec(i / 4 + 2);
            //指定元件所在列
            Spec columnSpec = GridLayout.spec(i % 4);
            //生成LayoutParams對象
            LayoutParams layoutParams = new LayoutParams(rowSpec, columnSpec);
            //指定元件充滿網格
            layoutParams.setGravity(Gravity.FILL);
            //将元件設定給GridLayout
            gridLayout.addView(button, layoutParams);
        }
    }
}
           

六. 絕對布局 AbsoluteLayout

1. 絕對布局介紹 

絕對布局特點 : 在絕對布局中,元件位置通過x, y坐标來控制, 布局容器不再管理元件位置, 大小, 這些都可以自定義; 

絕對布局使用情況 : 絕對布局不能适配不同的分辨率, 螢幕大小, 這種布局已經過時, 如果隻為一種裝置開發這種布局的話, 可以考慮使用這種布局;

2. 絕對布局的屬性

android:layout_x: 指定元件的x坐标;

android:layout_y: 指定元件的y坐标;

android:layout_width 是指定寬度是否充滿父容器, 或者僅僅包含子元素的,

android:width : 指定元件的寬度, 可以指定一個 數字 + 機關 , 如 100px 或者 100dp; 同理 android:layout_height 和 android:height;

3. 各種機關介紹

px : 像素, 每個px對應螢幕上的一個點;

dip/dp : device independent pixels, 裝置的獨立像素, 這種機關基于螢幕密度, 在每英寸160點的顯示器上 1dp = 1px, 随着螢幕密度改變, dp 與 px 換算會發生改變;

sp : scale pixels, 比例像素, 處理字型的大小, 可以根據使用者字型大小進行縮放;

in : 英寸, 标準長度機關

mm : 毫米, 标準長度機關

pt : 磅, 标準長度機關, 1/72英寸;

七. Android 分辨率 dip 與 px 轉換

【Android 應用開發】AndroidUI設計之 布局管理器 - 詳細解析布局實作一. 線性布局(LinearLayout)二. 相對布局RelativeLayout三. 幀布局FrameLayout四. 表格布局TableLayout五. 網格布局六. 絕對布局 AbsoluteLayout七. Android 分辨率 dip 與 px 轉換七. 擷取View對象寬高

1. 術語介紹

px : pixel, 像素, 螢幕分辨率就是像素, 分辨率用 寬度 * 長度 表示, 分辨率不是長寬比, Android中一般不直接處理分辨率;

density : 密度, 是以分辨率為基礎, 沿長寬方向排列的像素,密度低的螢幕像素少,密度高的螢幕像素多; 如果以像素為機關, 同一個按鈕在高密度螢幕 要比 在低密度螢幕要大.

dip : device independent pixel, 裝置獨立像素, 程式用dip來定義界面元素,dip與實際密度無關.

2. 螢幕密度與大小

手機螢幕密度分類 : 高 hdpi 240 , 中 mdpi 160, 小 ldpi 120, 在res下有對應密度的标簽資源, 注意這些資源與螢幕大小無關;

手機螢幕大小分類 : 大螢幕 4.8英寸以上, 普通螢幕 3.0 ~ 4.0英寸, 小螢幕 2.6 ~ 3.0英寸;

基準螢幕 : 正常尺寸, 與中密度120dpi,HAVG 320 * 480 是基準螢幕, 中密度 px == dip;

3. dip 與 px 換算

dip -> px :px = dip * (densityDpi / 160);

px -> dip :dip = px / (densityDpi / 160);

在中密度 mdpi 下, dip == px;

在高密度 hdpi 下, px > dip;

在低密度 ldpi 下, px < dip;

擷取密度 :DisplayMetrics dm = getResources().getDisplayMetrics();

密度 : int density =dm.densityDpi;

像素 :dm.widthPixel * dm.heightPixel;  

七. 擷取View對象寬高

如果在Activity中直接調用View元件的寬高, 獲得的寬高一定是0;

重寫 onWindowFocusChanged() .方法, 在這個方法中擷取寬高, 就能成功擷取到view元件的準确寬高值;

參考 : http://blog.csdn.net/sodino/article/details/10086633

.

作者 :萬境絕塵 

轉載請注明出處 : http://blog.csdn.net/shulianghan/article/details/18964835

.

,

,

,