大家好,我是豹哥,下面豹哥將會持續發布有關Android筑基系列的文章,希望在鞏固自己基礎的同時,可以幫到大家,友愛互融,共同進步😘🤞
文章目錄
- 知識點
- 未使用layout_weight
- 使用layout_weight
知識點
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>
獨學而無友,則孤陋而寡聞,歡迎大家對博主的文章提提建議,也歡迎私信與評論哦😘🤞
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/286789.html
標籤:其他
上一篇:Android中 呼叫系統攝像機&&回呼上傳(AndroidX庫)
下一篇:Android11中相機的變化
