安卓线性布局(属性)
安卓六大布局:
AbsoluteLayout 绝对布局(已过期,不建议使用)
RelativeLayout 相对布局
LinearLayout 线性布局
FrameLayout 帧布局
TableLayout 表格布局 (是LinerLayout的子类)
GridLayout 网格布局(4.0推出)
安卓目前使用的布局UI控件网状图:(该图箭头所指向的是其父类)
目前推荐使用RelativeLayout;LinearLayout;FrameLayout三个布局
<1>线性布局 LinearLayout布局
线性布局控制其中的控件按照横向或纵向方式排列,并且线性布局不会换行,当控件
排列到窗体边缘,后面的控件就被隐藏,不会被显示出来。
线性布局的默认方向是水平方向(Hoizontal)
线性布局的属性: match parent :匹配父容器的大小
wrap content :根据容器内的东西决定组件的大小
android:orientation(确定方向):定义布局内控件或组件的排列方式 vertical(垂直)horizontal(水平)
android:background: 设置控件的背景颜色或背景图片
android:id 设置控件的Id。这样就可以在R.java中生成相应的值,在程序中通过findViewById就可以调用 android:id = "@+id/id的名字"
gravity android:gravity="center":指定当前控件中内容(子控件)的对齐方式
android:layout_gravity="center":当前控件在其父控件中的对齐方式
代码注释:
<?xml version="1.0" encoding="UTF-8"?>
<!-- LinearLayout:线性布局,只能沿着水平或者垂直一个方向摆放控件,默认是沿着水平方向摆放控件 android:orientation="horizontal":水平方向,只能沿着水平方向摆放控件(一列只能放一个控件),是默认值 android:orientation="vertical":垂直方向,只能沿着垂直方向摆放控件(一行只能放一个控件) android:layout_width="match_parent" -->
-<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- android:layout_weight="1":权重(比例),如果不设置,权重值默认为0,默认情况下,手机渲染控件时,会首先 显示权重值小的控件,然后在显示稍大一点的控件 当线性布局的方向为垂直方向时,如果指定控件的高度是match_parent,则此时指定控件的权重值和其占据空间大小成反比. 当线性布局的方向为垂直方向时,如果指定控件的高度是wrap_content,则此时指定控件的权重值和其占据空间大小成正比. -->
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="One" android:textSize="30sp" android:background="@color/colorAccent" android:layout_weight="1"/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Two" android:textSize="30sp" android:background="@color/colorPrimary" android:layout_weight="2"/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Three" android:textSize="30sp" android:background="@android:color/holo_red_dark" android:layout_weight="3"/>
</LinearLayout>
TextView:文本显示控件
android:text:显示提示信息文本
EditText:文本输入控件,可以让用户输入自己的内容
android:hint=“请输入用户名”:当用户没有输入任何内容时显示的提示文本,用户一旦输入则提示文本自动消失,删除输入的字符后提示文本自动显示
android:id = "@+id/editText_userName" :将添加R类中的editText_userName静态内部类中的userName静态常量
android:layout_weight属性重点说明:
-
总结: 要正确使用weight,不要再去纠结
android:layout_width="match_parent"
android:layout_width="wrap_content" 两种情况下该如何设置weight. 因为这样设置根本就是错误的用法.
正确的用法是:
先设置 android:layout_width="0dp" 或者 android:layout_height="0dp"
然后再去调配权重
而此时的weight也非常好理解: weight就是比重!比例!请看下图
END