1、常用属性
1.1 id
设置资源id,在代码中可以通过findViewById获取组件
1.2 background
设置背景,可以是图片也可以是颜色
1.3 layout_width
设置宽度,match_parent
wrap_content
fill_parent
1.4 layout_height
设置高度,match_parent
wrap_content
fill_parent
1.5 orientation
设置布局中的组件排列方式,有horizontal
水平,vertical
垂直两种
1.6 gravity
控制内部组件的对齐方式,有top
bottom
left
right
center_vertical
(垂直方向居中), center_horizontal
(水平方向居中),
fill_vertical
(垂直方向拉伸), fill_horizontal
(水平方向拉伸),
center
, fill
, clip_vertical
, clip_horizontal
;
可以同时指定多种对齐方式 : 如 left|center_vertical
左侧垂直居中;
<?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:background="@color/wlback"
android:orientation="horizontal"
android:id="@+id/demo">
//组件
</LinearLayout>
2、权重
说明:按照权重等比例显示组件
2.1 水平android:layout_width="0dp"
,垂直android:layout_height="0dp"
,实现1:1的话就是分别设置android:layout_weight="1"
即可
<?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:background="@color/wlback"
android:orientation="horizontal"
android:id="@+id/demo">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="测试1"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="测试2"
android:layout_weight="1"/>
</LinearLayout>
2.2 如果组件本身就是高度或者宽度就是match_parent
、fill_parent
,计算方式就变了,a
代表组件的个数,b
代表屏幕减掉组件个数的差值b=1-a
,c
代表呀把屏幕分成几份,d代表其中一个组件占据的份额,那么占据屏幕空间是:1+b(d/6)
<?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:background="@color/wlback"
android:orientation="horizontal"
android:id="@+id/demo">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="测试1"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="测试2"
android:layout_weight="2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="测试3"
android:layout_weight="3"/>
</LinearLayout>
demo3占据控件:1-2*(1/6)= 2/3