安卓编程 之 线性布局
线性布局重要属性 android:orientation 方向,定位
可选属性:vertical 垂直的 horizontal 水平的
android:layout_weight 权重
android:layout_gravity 重力
本次学习的效果图如图右侧所示:
具体代码如下所示:每一行都加了我暂时的解释,以后有了新的见解在修改吧!肯定有理解不正确的。所谓的权重是只3个文本控件占据一行,把一行平均分成6份,权重值代表所占的份额,分子和分母都由我们填入的值来决定。
<?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" 高度:父容器的高度android:padding="20dp" 内边距:20像素 android:layout_margin="20dp"> 外边距:20像素<!--vertical 竖直的 horizontal 水平的--><TextView 文本控件android:layout_width="0dp" 宽度:0像素 android:layout_height="wrap_content" 高度:包裹内容android:text="按钮1" 文本:按钮1android:background="#FF00FF" 背景颜色:粉色android:textSize="20sp" 文本字号:20号android:textColor="#0000FF" 文本颜色:蓝色android:layout_weight="1" 控件权重:1/6 六分之一android:layout_gravity="bottom"/> 重力:底部位置<TextView 文本控件android:layout_width="0dp" 宽度:0像素 android:layout_height="wrap_content" 高度:包裹内容android:text="按钮2" 文本:按钮2android:background="#FFFF00" 背景颜色:黄色android:textSize="20sp" 文本字号:20号android:textColor="#FF0000" 文本颜色:红色android:layout_weight="2" 控件权重:2/6 六分之二android:layout_gravity="center"/> 重力:中心位置<TextView 文本控件android:layout_width="0dp" 宽度:0像素 android:layout_height="wrap_content" 高度:包裹内容android:text="按钮3" 文本:按钮3android:background="#FF0000" 背景颜色:红色android:textSize="20sp" 文本字号:20号android:textColor="#FFFF00" 文本颜色:黄色android:layout_weight="3" 控件权重:3/6 六分之三android:layout_gravity="top"/> 重力:顶部位置</LinearLayout> 线性布局j结束