安卓学习- 编写界面(页面布局详解)

1、线性布局
<? xml version= "1.0" encoding= "utf-8" ?>
< LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :orientation= "horizontal"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
< Button
android :id= "@+id/button_1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "Button 1"
/>
</ LinearLayout >


(1)排列方向:
android :orientation= "horizontal" 水平
android :orientation= "vertical" 垂直

(2)元素布局的对其方式:
对其方式,可选项为top、bottom、left、rirht、center,
中间可以用 | 来指定多个;
android :layout_gravity= "bottom"
注意:如果是vertical,就只有水平上的对齐才会生效

(3)边距
android :layout_marginTop= "10dp"

(4)比例权重
android :layout_weight= "1"
在某一个方向上设置权重之后,原本的那个方向上的数量置为0dp
< Button
android :id= "@+id/button_1"
android :layout_width= "0dp"
android :layout_weight= "1"
android :layout_height= "wrap_content"
android :text= "Button 1"
/>
< Button
android :id= "@+id/button_2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "Button 2"
/>


2、相对布局
(1)相对父布局
< Button
android :id= "@+id/button_1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentTop= "true"
android :layout_alignParentLeft= "true"
android :text= "Button 1"
/>
android :layout_alignParentBottom= "true"
android :layout_alignParentRight= "true"

android :layout_centerInParent= "true"

(2)相对控件布局
< Button
android :id= "@+id/button_3"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_centerInParent= "true"
android :text= "Button 3"
/>

< Button
android :id= "@+id/button_1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_above= "@id/button_3"
android :layout_toLeftOf= "@id/button_3"
android :text= "Button 1"
/>

android :layout_below= "@id/button_3"
android :layout_toRightOf= "@id/button_3"




3、帧布局(略过)
FrameLayout

4、百分比布局(新增布局)(PercentFrameLayout,PercentRelativeLayout)
打开app/build.gradle,在dependencies闭包中添加如下内容:
dependencies {
compile fileTree( dir : 'libs' , include : [ '*.jar' ])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:percent:24.2.1'
}

然后在布局中使用 PercentFrameLayout
<? xml version= "1.0" encoding= "utf-8" ?>
< android.support.percent.PercentFrameLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: app = "http://schemas.android.com/apk/res-auto"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >

< Button
android :id= "@+id/button1"
android :text= "Button 1"
android :layout_gravity= "left|top"
app :layout_widthPercent= "50%"
app :layout_heightPercent= "50%"
/>

< Button
android :id= "@+id/button2"
android :text= "Button 2"
android :layout_gravity= "right|top"
app :layout_widthPercent= "50%"
app :layout_heightPercent= "50%"
/>

< Button
android :id= "@+id/button3"
android :text= "Button 3"
android :layout_gravity= "left|bottom"
app :layout_widthPercent= "50%"
app :layout_heightPercent= "50%"
/>

< Button
android :id= "@+id/button4"
android :text= "Button 4"
android :layout_gravity= "right|bottom"
app :layout_widthPercent= "50%"
app :layout_heightPercent= "50%"
/>
</ android.support.percent.PercentFrameLayout >








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值