Android LinearLayout线性布局

线性布局LinearLayout是一个视图容器,用于使所有子视图在单个方向保持对齐
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="200dp"
        android:layout_height="90dp"
        android:background="#4593Ec"
        android:gravity="center"
        android:text="1"
        android:textColor="@color/white"
        android:textSize="25sp"/>

    <TextView
        android:layout_width="100dp"
        android:layout_height="90dp"
        android:background="#7DB145"
        android:gravity="center"
        android:text="2"
        android:textColor="@color/white"
        android:textSize="25sp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="90dp"
        android:background="#F10000"
        android:gravity="center"
        android:text="3"
        android:textColor="@color/white"
        android:textSize="25sp"/>

</LinearLayout>

android:orientation="vertical" 垂直方向下(图1)
android:orientation="horizontal" 水平方向下(图2)

在这里插入图片描述
在这里插入图片描述

布局权重:通过给子视图设置权重值,来分配子视图所占空间的权重(比例)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#3c3f41"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#ff5722"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#4795ec"/>

</LinearLayout>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/SeNiLS/article/details/114735497