(android筑基系列)之LinearLayout适配问题(android:layout_weight属性)

Posted 豹豹-Boss成

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(android筑基系列)之LinearLayout适配问题(android:layout_weight属性)相关的知识,希望对你有一定的参考价值。

大家好,我是豹哥,下面豹哥将会持续发布有关android筑基系列的文章,希望在巩固自己基础的同时,可以帮到大家。友爱互融,共同进步😘🤞

文章目录

知识点

android:layout_weight是LinearLayout中的一个重要的属性,使用这个属性,我们可以让屏幕的适配性变得更好,不会出现上面稀疏松垮,下面密集堆积的情况。关于这个属性的用法,要注意的就是不能思维局限,不仅可以在各种控件中使用,还可以在布局的嵌套中使用。有关这个属性的原理,简单的说,就是分蛋糕(假如有两个控件,每个的layout_weight都设置为1,那么每个控件所分得的蛋糕就是1/2)

未使用layout_weight

下面用一个例子来展示一下不适配的UI有多丑

使用layout_weight

虽然博主做的UI不太好看,但是添加了android:layout_weight后明显就赏心悦目多了🤣😜


下面呈上博主的丑代码😂🤣😜

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/greenyellow"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="温度系统"
        android:textAlignment="center"
        android:textSize="30dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">


        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text=" 温度"
            android:textColor="@color/blueviolet"
            android:textSize="40sp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/temp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text="@string/temp_value"
            android:textColor="@color/red"
            android:textSize="50sp" />
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/greenyellow"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="湿度系统"
        android:textAlignment="center"
        android:textSize="30dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">


        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text="湿度"
            android:textColor="@color/blueviolet"
            android:textSize="40sp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/humi" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text="@string/humi_value"
            android:textColor="@color/red"
            android:textSize="50sp" />


    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/greenyellow"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="液位系统"
        android:textAlignment="center"
        android:textSize="30dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">


        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text="液位"
            android:textColor="@color/blueviolet"
            android:textSize="40sp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/liquid_level" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text="@string/liquid_value"
            android:textColor="@color/red"
            android:textSize="50sp" />


    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/greenyellow"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="PH系统"
        android:textAlignment="center"
        android:textSize="30dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">


        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:gravity="center"
            android:text="PH"
            android:textColor="@color/blueviolet"
            android:textSize="40sp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ph" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:gravity="center"
            android:text="@string/PH_value"
            android:textColor="@color/red"
            android:textSize="50sp" />


    </LinearLayout>

</LinearLayout>

独学而无友,则孤陋而寡闻。欢迎大家对博主的文章提提建议。也欢迎私信与评论哦😘🤞

以上是关于(android筑基系列)之LinearLayout适配问题(android:layout_weight属性)的主要内容,如果未能解决你的问题,请参考以下文章

(android筑基系列)之LinearLayout适配问题(android:layout_weight属性)

(android筑基系列)之LinearLayout适配问题(android:layout_weight属性)

Java筑基之Java简介

一线互联网架构师筑基必备技能之Java篇,不愧是大佬

一文详解:中信银行java笔试题库

一文详解:中信银行java笔试题库