一手遮天 Android - view(布局类): LinearLayout 线性布局

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(布局类): LinearLayout 线性布局

示例如下:

/view/layout/LinearLayoutDemo1.java

/**
 * LinearLayout - 线性布局控件
 */

package com.webabcd.androiddemo.view.layout;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.webabcd.androiddemo.R;

public class LinearLayoutDemo1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_layout_linearlayoutdemo1);

        // 演示如何在 java 中控制 LinearLayout 布局,仅代码演示,没有对应的显示效果
        sample();
    }

    private void sample() {
        LinearLayout linearLayout = new LinearLayout(this);
        // 对应 xml 中的 orientation
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        // 对应 xml 中的 divider
        linearLayout.setDividerDrawable(getResources().getDrawable(R.drawable.img_sample_son));
        // 对应 xml 中的 showDividers
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_BEGINNING);
        int dpValue = 10;
        float scale = this.getResources().getDisplayMetrics().density;
        int pxValue = (int) (dpValue * scale + 0.5f);
        // 对应 xml 中的 dividerPadding
        linearLayout.setDividerPadding(pxValue);

        TextView textView = new TextView(this);
        // 第 1 个参数对应 xml 中的 layout_width(像素值)
        // 第 2 个参数对应 xml 中的 layout_height(像素值)
        // 第 3 个参数对应 xml 中的 layout_weight
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                1.0f);
        textView.setLayoutParams(layoutParams);

        linearLayout.addView(textView);
    }
}

/layout/activity_view_layout_linearlayoutdemo1.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
    LinearLayout - 线性布局控件
        orientation - 排列方向(vertical 或 horizontal)
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
        layout_weight - 空间权重
            以本例来说,LinearLayout 中的 3 个控件在排列方向上采用 wrap_content 占位
            1、LinearLayout 中的每个控件占用了 0 空间
            2、可用于分配的空间为 match_parent - 0 = match_parent
            3、LinearLayout 中所有控件的 layout_weight 的和为 6,第一个控件的 layout_weight 为 1
            4、所以第一个控件的空间权重为 0 + 1/6 * match_parent
            5、所以第二个控件的空间权重为 0 + 2/6 * match_parent
            6、所以第三个控件的空间权重为 0 + 3/6 * match_parent
    -->
    <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal">
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" />
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:background="@color/green" />
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:background="@color/blue" />
    </LinearLayout>

    <!--
        layout_weight - 空间权重
            以本例来说,LinearLayout 中的 3 个控件在排列方向上采用 match_parent 占位
            1、LinearLayout 中的每个控件占用了 match_parent 空间
            2、可用于分配的空间为 match_parent - 3 * match_parent = -2 * match_parent
            3、LinearLayout 中所有控件的 layout_weight 的和为 6,第一个控件的 layout_weight 为 1
            4、所以第一个控件的空间权重为 match_parent + 1/6 * (-2 * match_parent) =  4/6 * match_parent
            5、所以第二个控件的空间权重为 match_parent + 2/6 * (-2 * match_parent) =  2/6 * match_parent
            6、所以第三个控件的空间权重为 match_parent + 3/6 * (-2 * match_parent) =  0 * match_parent
    -->
    <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:layout_marginTop="10dp">
        <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" />
        <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:background="@color/green" />
        <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:background="@color/blue" />
    </LinearLayout>

    <!--
        layout_weight - 空间权重
            以本例来说(参考本文件的第一个示例),指定了 LinearLayout 中的所有控件的总的空间权重 weightSum 为 10
            1、所以第一个控件的空间权重为 0 + 1/10 * match_parent
            2、所以第二个控件的空间权重为 0 + 2/10 * match_parent
            3、所以第三个控件的空间权重为 0 + 3/10 * match_parent
    -->
    <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:layout_marginTop="10dp" android:weightSum="10">
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" />
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:background="@color/green" />
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:background="@color/blue" />
    </LinearLayout>

    <!--
        divider - 分隔线的资源
        showDividers - 分隔线的显示方式
            none - 不显示
            beginning - 在第一个控件的起始处显示分隔线
            middle - 在每个控件之间显示分隔线
            end - 在最后控件的结尾处显示分隔线
        dividerPadding  - 分隔线的 padding
            水平排列就是上下 padding
            垂直排列就是左右 padding
    -->
    <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:layout_marginTop="10dp"
        android:divider="@drawable/shape_linearlayout_divider_vertical" android:showDividers="beginning|middle|end" android:dividerPadding="10dp"  >
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" />
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:background="@color/green" />
        <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:background="@color/blue" />
    </LinearLayout>

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 12:34  webabcd  阅读(78)  评论(0编辑  收藏  举报