UI布局 4种布局方法

 

布局产生器LayoutInflater

获取布局产生器的方法
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
LayoutInflater.from(Context)

1 //获取布局产生器(方法一),通过 getSystemService 方法返回系统级的处理程序实例
2  LayoutInflater inflater = = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3  
4  //获取布局产生器(方法二)
5  LayoutInflater inflater = LayoutInflater.from(Context);
6  
7  View layout = inflater.inflate(R.layout.layoutinflater_demo1, null);

 

 

 

 

线性布局LinearLayout

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent" 
 5     android:layout_height="match_parent" 
 6     android:background="@drawable/bg0">
 7     <!--  
 8         android:id                -    为控件指定相应的ID
 9         android:text            -    指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml
10         android:gravity            -    指定控件文本内容的基本位置,比如说居中,居右等位置
11         android:textSize        -    指定控件当中文本内容的字体大小
12         android:background        -    指定该控件所使用的背景色,RGB命名法
13         android:layout_width    -    指定控件的宽度
14         android:layout_height    -    指定控件的高度
15         android:padding            -    指定控件的内边距,也就是说控件当中的文本内容与控件边框的距离
16         android:paddingTop        -    指定控件当中的文本内容与控件顶部边框的距离
17         android:paddingBottom    -    指定控件当中的文本内容与控件底部边框的距离
18         android:paddingLeft        -    指定控件当中的文本内容与控件左边框的距离
19         android:paddingRight    -    指定控件当中的文本内容与控件右边框的距离
20         android:layout_weight    -    指定控件占屏幕布局的比例,默认为0,根据内容决定占据的空间大小>0,按照权重比例占据空间
21         android:sigleLine        -    如果设置为 true,当控件中的内容在同一行显示不了的情况下,不换行
22                                 -    如果设置为 false,当控件中的内容在同一行显示不了的情况下,自动换行显示,并且会撑开控件大小
23     -->
24     <TextView
25         android:id="@+id/tvFirst"
26         android:text="@string/tvFirst"
27         android:textSize="30dp"
28         android:background="#aa0000"
29         android:layout_width="match_parent" 
30         android:layout_height="wrap_content"
31         android:layout_weight="1"
32         android:singleLine="true"/>
33     <TextView
34         android:id="@+id/tvSecond"
35         android:text="第二行"
36         android:textSize="30dp"
37         android:background="#aa00aa"
38         android:layout_width="match_parent" 
39         android:layout_height="wrap_content"
40         android:paddingLeft="10dp"
41         android:paddingRight="30dp"
42         android:layout_weight="1"
43         android:singleLine="true"/>
44     <TextView
45         android:id="@+id/tvThird"
46         android:text="第三行"
47         android:gravity="center_horizontal|center_vertical"
48         android:textSize="30dp"
49         android:background="#0000aa"
50         android:layout_width="match_parent" 
51         android:layout_height="wrap_content"
52         android:layout_weight="1"
53         android:singleLine="true"/>
54 </LinearLayout>

 

 

 相对布局RelativeLayout

 

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <!-- 
  3     1、当前控件布局在指定控件的上面、下面、左边、右边
  4     android:layout_above                   -    将该控件的底部置于给定ID的控件之上
  5     android:layout_below                   -    将该控件的顶部置于给定ID的控件之下
  6     android:layout_toLeftOf                   -    将该控件置于给定ID控件的左边
  7     android:layout_toRightOf               -    将该控件置于给定ID控件的右边
  8     
  9     2、当前控件的基准线(控件高度除以2的水平线)、上边缘、下边缘、左边缘、右边缘与指定控件的基准线、上边缘、下边缘、左边缘、右边缘对齐
 10     android:layout_alignBaseline           -    将该控件的baseline和给定的控件的baseline对齐
 11     android:layout_alignTop                  -    将该控件的顶部边缘和给定ID控件的顶部边缘对齐
 12     android:layout_alignBottom               -    将该控件的底部边缘和给定ID控件的底部边缘对齐
 13     android:layout_alignLeft               -    将该控件的左边缘和给定ID控件的左边缘对齐
 14     android:layout_alignRight               -    将该控件的右边缘和给定ID控件的右边缘对齐
 15     
 16     3、当前控件的上边缘、下边缘、左边缘、右边缘与父控件的上边缘、下边缘、左边缘、右边缘对齐
 17     android:layout_alignParentTop          -    如果该值为true,则将该控件的顶部和父控件顶部对齐
 18     android:layout_alignParentBottom    -    如果该值为true,则将该控件的底部和父控件的底部对齐
 19     android:layout_alignParentLeft         -    如果该值为true,则将该控件的左边和父控件的左边对齐
 20     android:layout_alignParentRight        -    如果该值为true,则将该控件的右边和父控件的右边对齐
 21     
 22     4、当前控件布局在父控件的:水平方向的中央、垂直方法的中央、水平方向和垂直方向的中央
 23     android:layout_centerHorizontal        -    如果该值为true,则该控件将被置于父控件水平方向的中央
 24     android:layout_centerVertical        -    如果该值为true,则该控件将被置于父控件垂直方法的中央
 25     android:layout_centerInParent        -    如果该值为true,则该控件将被置于父控件水平方向和垂直方向的中央
 26     
 27     5、当前控件:上边缘、下边缘、左边缘、右边缘的外部填充距离
 28     android:layout_margin                -    设置顶部外边缘、底部外边缘、左外边缘、右外边缘的距离
 29     android:layout_marginTop            -    设置控件的顶部外边缘距离
 30     android:layout_marginBottom            -    设置控件的底部外边缘距离
 31     android:layout_marginLeft            -    设置控件的左外边缘距离
 32     android:layout_marginRight            -    设置控件的右外边缘距离
 33  -->
 34  
 35 <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
 36     android:layout_width="match_parent"
 37     android:layout_height="match_parent">
 38     <TextView 
 39         android:id="@+id/tvInput"
 40         android:layout_width="wrap_content"
 41         android:layout_height="wrap_content"
 42         android:text="请输入:"/>
 43     <EditText 
 44         android:id="@+id/etMsg"
 45         android:layout_width="match_parent"
 46         android:layout_height="wrap_content"
 47         android:hint="请输入信息"
 48         android:layout_below="@+id/tvInput"
 49         android:layout_alignParentLeft="true"/>
 50     
 51     <Button 
 52         android:id="@+id/btnCancel"
 53         android:layout_width="wrap_content"
 54         android:layout_height="wrap_content"
 55         android:layout_below="@+id/etMsg"
 56         android:layout_alignParentRight="true"
 57         android:text="取消"/>
 58     <Button 
 59         android:id="@+id/btnOk1"
 60         android:layout_width="wrap_content"
 61         android:layout_height="wrap_content"
 62         android:layout_below="@+id/etMsg"
 63         android:layout_toLeftOf="@+id/btnCancel"
 64         android:text="确定1"/>
 65     <Button 
 66         android:id="@+id/btnOk2"
 67         android:layout_width="wrap_content"
 68         android:layout_height="40dp"
 69         android:layout_toLeftOf="@+id/btnOk1"
 70         android:layout_alignTop="@+id/btnOk1"
 71         android:text="确定2"/>
 72     <Button 
 73         android:id="@+id/btnOk3"
 74         android:layout_width="wrap_content"
 75         android:layout_height="wrap_content"
 76         android:layout_toLeftOf="@+id/btnOk2"
 77         android:layout_alignBaseline="@+id/btnOk2"
 78         android:text="确定3"/>
 79      
 80         <!-- 对比 layout_alignTop、layout_alignBaseline设置后,对后序相对定位 android:layout_below 的影响,
 81         结合btnOk2、btnOk3、btnOk4演示 -->
 82    
 83    
 84     <Button 
 85         android:id="@+id/btnOk4"
 86         android:layout_width="wrap_content"
 87         android:layout_height="wrap_content"
 88         android:layout_below="@+id/btnOk2"
 89         android:layout_alignLeft="@+id/btnOk3"
 90         android:text="确定4"/>
 91   
 92     <Button 
 93         android:id="@+id/btnOk5"
 94         android:layout_width="wrap_content"
 95         android:layout_height="wrap_content"
 96         android:layout_centerHorizontal="true"
 97         android:text="水平中央"/>
 98     <Button 
 99         android:id="@+id/btnOk6"
100         android:layout_width="wrap_content"
101         android:layout_height="wrap_content"
102         android:layout_centerVertical="true"
103         android:text="垂直中央"/>
104     <Button 
105         android:id="@+id/btnOk7"
106         android:layout_width="wrap_content"
107         android:layout_height="wrap_content"
108         android:layout_centerInParent="true"
109         android:text="水平且垂直中央"/>
110     <Button 
111         android:id="@+id/btnOk8"
112         android:layout_width="wrap_content"
113         android:layout_height="wrap_content"
114         android:layout_below="@+id/btnOk7"
115         android:text="确认8"/>
116     <Button 
117         android:id="@+id/btnOk9"
118         android:layout_width="wrap_content"
119         android:layout_height="wrap_content"
120         android:layout_below="@+id/btnOk7"
121         android:layout_toRightOf="@+id/btnOk8"
122         android:text="确认9"/>
123     <Button 
124         android:id="@+id/btnOk10"
125         android:layout_width="wrap_content"
126         android:layout_height="wrap_content"
127         android:layout_below="@+id/btnOk7"
128         android:layout_toRightOf="@+id/btnOk9"
129         android:text="确认10"/>
130     <Button 
131         android:id="@+id/btnOk11"
132         android:layout_width="wrap_content"
133         android:layout_height="wrap_content"
134         android:layout_below="@+id/btnOk8"
135         android:layout_marginLeft="20dp"
136         android:text="确认11"/>
137     <Button 
138         android:id="@+id/btnOk12"
139         android:layout_width="wrap_content"
140         android:layout_height="wrap_content"
141         android:layout_below="@+id/btnOk8"
142         android:layout_toRightOf="@+id/btnOk11"
143         android:text="确认12"/>
144     <Button 
145         android:id="@+id/btnOk13"
146         android:layout_width="wrap_content"
147         android:layout_height="wrap_content"
148         android:layout_below="@+id/btnOk8"
149         android:layout_toRightOf="@+id/btnOk12"
150         android:layout_marginTop="20dp"
151         android:text="确认13"/>
152     <Button 
153         android:id="@+id/btnOk14"
154         android:layout_width="wrap_content"
155         android:layout_height="wrap_content"
156         android:layout_below="@+id/btnOk8"
157         android:layout_toRightOf="@+id/btnOk13"
158         android:layout_margin="10dp"
159         android:text="确认14"/>
160 </RelativeLayout>

 

 

 

表格布局TableLayout

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:stretchColumns="0,1,2">
 6     <TableRow>
 7         <TextView 
 8             android:text="@string/tvRow1_Column1"
 9             android:gravity="center"
10                android:background="#0000bb"
11                android:layout_margin="1dp"/>
12         <TextView 
13             android:text="@string/tvRow1_Column2"
14             android:gravity="center"
15                android:background="#0000bb"
16                android:layout_margin="1dp"/>
17         <TextView 
18             android:text="@string/tvRow1_Column3"
19             android:gravity="center"
20                android:background="#0000bb"
21                android:layout_margin="1dp"/>
22     </TableRow>
23     <TableRow>
24         <TextView 
25             android:text="@string/tvRow2_Column1"
26             android:gravity="center"
27                android:background="#0000bb"
28                android:layout_margin="1dp"/>
29         <TextView 
30             android:text="@string/tvRow2_Column2"
31             android:gravity="center"
32                android:background="#0000bb"
33                android:layout_margin="1dp"/>
34         <TextView 
35             android:text="@string/tvRow2_Column3"
36             android:gravity="center"
37                android:background="#0000bb"
38                android:layout_margin="1dp"/>
39     </TableRow>
40 </TableLayout>

 

 

框架布局FrameLayout

 

 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     <TextView 
 6         android:id="@+id/tv1"
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="第1层"
10         android:textColor="#FF3366"
11         android:textSize="10dp"/>
12     <TextView 
13         android:id="@+id/tv2"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="第2层"
17         android:textColor="#6666FF"
18         android:textSize="20dp"/>
19     <TextView 
20         android:id="@+id/tv3"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:text="第3层"
24         android:textColor="#FFFF00"
25         android:textSize="30dp"/>
26 </FrameLayout>

 

 

转载于:https://www.cnblogs.com/totome/archive/2012/08/30/2664334.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值