您的位置:首页 > 移动开发 > Android开发

Androidx学习笔记(6)--常见布局--线性布局

2016-01-19 20:00 411 查看

线性布局

LinearLayout

指定各个节点的排列方向
android:orientation="horizontal"


设置右对齐
android:layout_gravity="right"


当竖直布局时,只能左右对齐和水平居中,顶部底部对齐竖直居中无效
当水平布局时,只能顶部底部对齐和竖直居中
使用match_parent时注意不要把其他组件顶出去

线性布局非常重要的一个属性:权重
android:layout_weight="1"


权重设置的是按比例分配剩余的空间,并且要设置layout_width或layout_height要为0

有一个布局方向,水平或者竖直
在竖直布局下,左对齐、右对齐,水平居中生效
在水平布局下,顶部对齐、底部对齐、竖直居中生效
权重:按比例分配屏幕的剩余宽度或者高度

示例:



布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_weight="1"android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal">
<TextView
android:layout_weight="1"android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ff0000"
/>
<TextView
android:layout_weight="1"android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ffffff"
/>
<TextView
android:layout_weight="1"android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#000000"
/>
<TextView
android:layout_weight="1"android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
>
<TextView
android:layout_weight="1"android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#00ff00"
/>
<TextView
android:layout_weight="1"android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/darker_gray"
/>
<TextView
android:layout_weight="1"android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#000000"
/>
<TextView
android:layout_weight="1"android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffcc0000"
/>
</LinearLayout>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: