android学习日记12--布局管理器
1、概述
布局管理器的用途:
a、可以更好的管理組件;
b、通過使用布局管理器,Android應用程序可以做到平臺無關性
布局管理器都是ViewGroup的子類,所有可充當容器的父類都是ViewGroup,而ViewGroup也是View的子類
下面分別介紹常用的布局管理器
?
2、線性布局管理器
LinearLayout,最常用的布局之一。它提供控件水平或垂直排列的模型
常用屬性及其對應方法:
gravity 可取屬性說明:
當需要為gravity設多個值時,可用|分隔開
布局XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="horizontal" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:id="@+id/lla" 7 android:gravity="right" 8 > 9 10 <Button 11 android:text="添加" 12 android:id="@+id/Button01" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content"> 15 </Button> 16 17 </LinearLayout> View CodeJAVA代碼:
1 // 計數器,記錄按鈕個數 2 int count = 0; 3 4 @Override 5 public void onCreate(Bundle savedInstanceState) { // 重寫 onCreate 方法 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.horizontal_layout); 8 // 獲取屏幕中的按鈕控件對象 9 Button button = (Button) findViewById(R.id.Button01); 10 11 // 為按鈕添加 OnClickListener 接口實現 12 button.setOnClickListener( 13 14 15 new View.OnClickListener() { 16 17 public void onClick(View v) { 18 // 獲取線性布局對象 19 LinearLayout ll = (LinearLayout) findViewById(R.id.lla); 20 21 String msg = MainActivity.this.getResources().getString( 22 R.string.button); 23 // 創建一個 Button 對象 24 Button tempbutton = new Button(MainActivity.this); 25 26 27 28 tempbutton.setText(msg + (++count)); // 設置 Button 控件顯示的內容 29 // 設置 Button 的寬度 30 tempbutton.setWidth(80); 31 // 向線性布局中添加 View 32 ll.addView(tempbutton); 33 34 35 36 } 37 38 }); 39 40 } View Code運行效果:每點擊添加按鈕一次會在下方垂直生成一個按鈕
?
將布局文件中
android:orientation="vertical" ??
vertical改為horizontal
每點擊一次會在右方水平方向生成一個按鈕
當水平方向該行容不下一個寬度為80的按鈕時,按鈕就會被壓縮,如下圖
此時再點擊添加按鈕時,畫面沒有任何變化,不會另起一行添加按鈕,超出屏幕的將不會被顯示。
?
3、表格布局
TableLayout 類似HTML里的Table分為行和列來管理。
每一行為一個TableRow,也可以為View對象。當為View對象時就跨越該行所有列
TableRow中可以添加子控件,每個子控件為一列。并不會為每個單元格繪制邊框
每個單元格為一個View,可以有空的單元格,也可以跨越多列
一個列的寬度由該列最寬的單元格決定的
TableLayout 可以設置三種屬性
Shrinkable :它可以被壓縮以適應其父容器的大小
Stretchable :它可以被拉伸以填滿空閑區域
Collapsed :該列被隱藏
如果要對多列進行設置,用逗號隔開
這三個屬性在JAVA代碼也有對應的方法,值得一提的是它是繼承Linearlayout的
布局XML:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/LinearLayout01" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:background="@drawable/water" 6 android:gravity="bottom" 7 android:orientation="vertical" > 8 9 <TableLayout 10 android:id="@+id/TableLayout01" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" > 13 14 <TextView 15 android:id="@+id/TextView01" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_centerInParent="true" 19 android:layout_margin="4px" 20 android:background="@drawable/darkgray" 21 android:text="@string/tv1" > 22 </TextView> 23 </TableLayout> 24 25 <TableLayout 26 android:id="@+id/TableLayout02" 27 android:layout_width="fill_parent" 28 android:layout_height="wrap_content" 29 android:stretchColumns="0" > 30 31 <TableRow 32 android:id="@+id/TableRow01" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" > 35 36 <TextView 37 android:id="@+id/TextView02" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:layout_centerInParent="true" 41 android:layout_margin="4px" 42 android:background="@drawable/blue" 43 android:text="@string/tvStrech" > 44 </TextView> 45 46 <TextView 47 android:id="@+id/TextView03" 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:layout_centerInParent="true" 51 android:layout_margin="4px" 52 android:text="@string/tvShort" > 53 </TextView> 54 </TableRow> 55 </TableLayout> 56 57 <TableLayout 58 android:id="@+id/TableLayout03" 59 android:layout_width="fill_parent" 60 android:layout_height="wrap_content" 61 android:collapseColumns="1" 62 android:shrinkColumns="0" > 63 64 <TableRow 65 android:id="@+id/TableRow02" 66 android:layout_width="wrap_content" 67 android:layout_height="wrap_content" > 68 69 <TextView 70 android:id="@+id/TextView04" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" 73 android:layout_centerInParent="true" 74 android:layout_margin="4px" 75 android:background="@drawable/darkgray" 76 android:text="@string/tvShrink" > 77 </TextView> 78 79 <TextView 80 android:id="@+id/TextView05" 81 android:layout_width="wrap_content" 82 android:layout_height="wrap_content" 83 android:layout_centerInParent="true" 84 android:layout_margin="4px" 85 android:background="@drawable/lightred" 86 android:text="@string/tvShort" > 87 </TextView> 88 89 <TextView 90 android:id="@+id/TextView06" 91 android:layout_width="wrap_content" 92 android:layout_height="wrap_content" 93 android:layout_centerInParent="true" 94 android:layout_margin="4px" 95 android:background="@drawable/blue" 96 android:text="@string/tvLong" > 97 </TextView> 98 </TableRow> 99 </TableLayout> 100 101 </LinearLayout> View Code運行效果:
?
4、相對布局
RelativeLayout子控件 的位置由兄弟控件或父容器來決定的
如果A控件由B控件來決定位置,則布局文件B控件要在A控件聲明之前
常用屬性
第一類:屬性值為true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對于父元素完全居中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft 貼緊父元素的左邊緣
android:layout_alignParentRight 貼緊父元素的右邊緣
android:layout_alignParentTop 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物
第二類:屬性值必須為id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左邊
android:layout_toRightOf 在某元素的右邊
android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
第三類:屬性值為具體的像素值,如30dip,40px
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
布局XML:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="@string/hello_world" 15 android:id="@+id/tv1" 16 android:layout_centerInParent="true" 17 /> 18 19 <TextView 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="big" 23 android:textSize="30sp" 24 android:id="@+id/tv2" 25 android:layout_toRightOf="@id/tv1" 26 android:layout_alignBottom="@id/tv1" 27 /> 28 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="middle" 33 android:textSize="20sp" 34 android:id="@+id/tv3" 35 android:layout_above="@id/tv1" 36 android:layout_alignLeft="@id/tv1" 37 /> 38 39 </RelativeLayout> View Code運行效果:
?
5、幀布局
FrameLayout在屏幕上開辟一塊區域,在這塊區域可以添加多個控件
但都會被對其到屏幕左上角,并且大小取決于最大的控件,如果控件一樣大,只能看到最上面的控件
布局XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="fill_parent" 10 android:background="@drawable/lightgray" 11 android:gravity="center" 12 android:text="big" /> 13 14 <TextView 15 android:layout_width="150dp" 16 android:layout_height="150dp" 17 android:background="@drawable/darkgray" 18 android:gravity="center" 19 android:text="middle" /> 20 21 <TextView 22 android:layout_width="50dp" 23 android:layout_height="50dp" 24 android:background="@drawable/blue" 25 android:gravity="center" 26 android:text="small" /> 27 28 </FrameLayout> View Code運行效果:
?
6、絕對布局
AbsoluteLayout是絕對位置布局。在此布局中的子元素的android:layout_x和android:layout_y屬性將生效,用于描述該子元素的坐標位置。
屏幕左上角為坐標原點(0,0),第一個0代表橫坐標,向右移動此值增大,第二個0代表縱坐標,向下移動,此值增大。
在此布局中的子元素可以相互重疊。在實際開發中,通常不采用此布局格式,因為它的界面代碼過于剛性,以至于有可能不能很好的適配各種終端。
由于不采用,在此就不演示代碼了。
轉載于:https://www.cnblogs.com/aiguozhe/p/3577371.html
總結
以上是生活随笔為你收集整理的android学习日记12--布局管理器的全部內容,希望文章能夠幫你解決所遇到的問題。