Android LinearLayout线性布局(上下左右 正中间)示例

我先把整个窗口 进行了垂直对齐,把窗口分成了上中下三层,用到的是一个权重属性:android:layout_weight="1",就是按比例分配大小,上下设置为1,中间层设置8,然后再在每一层里面,对按钮进行布局,中间层里面还要嵌套一个线性布局,这是因为android:orientation="horizontal",这个布局方向的特点,默认是水平对齐,如果设置成水平对齐了,那么里面的元素就不能进行水平方向的修改(android:layout_gravity="center_horizontal"就没有效果),相反同理,所以就要用两层线性布局嵌套来解决。

下面是activity_main.xml
<LinearLayout 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:orientation="vertical" 

tools:context="${packageName}.${activityClass}" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="左上" />

<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:text="2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="右上" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="horizontal" >

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="中间" />

</LinearLayout>
</LinearLayout>

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom" 
>
<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="左下"
/>
<Button 
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:text="2"
/>
<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="右下"
/>

</LinearLayout>

</LinearLayout>



</LinearLayout>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值