学习LinearLayout(线性布局)

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_parentfill_parent,计算方式就变了,a代表组件的个数,b代表屏幕减掉组件个数的差值b=1-ac代表呀把屏幕分成几份,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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容