Android基礎(chǔ)知識及線性布局介紹
1.常見控件的基本屬性
android:id="@+id/button1":【設(shè)置控件id】
android:layout_width【設(shè)置控件寬度】/android:layout_height【設(shè)置控件高度】
wrap_content【控件的大小由內(nèi)部決定】
match_parent【控件的大小與父控件保持一致】
android:text=" ":【設(shè)置組件文本】
android:textColor=" ":【設(shè)置字體顏色】
android:layout_marginLeft:【當(dāng)前布局與父布局左邊緣的距離】
android:layout_marginRight:【當(dāng)前布局與父布局右邊緣的距離】
android:layout_marginTop:【當(dāng)前布局與父布局頂部邊緣的距離】
android:layout_marginBottom:【當(dāng)前布局與父布局底部邊緣的距離】
android:gravity :【view里面的內(nèi)容在這個view中的位置】
android:layout_gravity :【這個view相對于它父view的位置】
1、gravity在線性布局中不起任何作用,layout_gravity在線性布局中起作用;
2、 當(dāng)我們使用 android:orientation=“vertical” 時, android:layout_gravity只有水平方向的設(shè)置才起作用,
垂直方向的設(shè)置不起作用。即:left,right,center_horizontal 是生效的;
3、當(dāng) 我們使用android:orientation=“horizontal” 時, android:layout_gravity只有垂直方向的設(shè)置才起作用,
水平方向的設(shè)置不起作用。即:top,bottom,center_vertical 是生效的。
1.1控件的可見性
該屬性有三種狀態(tài)值:gone、visible、invisible。
gone 與invisible的區(qū)別是:
gone 表示控件不可見,也不會占任何的位置,也不會有任何響應(yīng)。
而invisible表示控件雖然不可見,但是會占據(jù)它的寬高位置。
例子:
<LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="button1"> </Button> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="invisible" //invisible表示控件雖然不可見,但是會占據(jù)它的寬高位置。 android:text="button2"> </Button> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="button3"></Button> </LinearLayout>
效果如圖:
例子:
<LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="button1"> </Button> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone" //gone 表示控件不可見,也不會占任何的位置,也不會有任何響應(yīng)。 android:text="button2"> </Button> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="button3"> </Button> </LinearLayout>
效果如圖:
1.2控件的外邊距
學(xué)習(xí)過HTML的都會知道CSS里的盒模式有個外邊距和內(nèi)邊距。
外邊距可以設(shè)置視圖距離父視圖上下左右的距離。
內(nèi)邊距可以設(shè)置視圖內(nèi)部內(nèi)容距離自己邊框上下左右的距離。
Android 的控件布局其實也用的是這個盒模式。
如果距離父視圖上下左右的外邊距相同,可以這么設(shè)置:
android:layout_margin="10dp"
我們也可以單獨的設(shè)置某個外邊距:
android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
1.3控件的內(nèi)邊距
統(tǒng)一設(shè)置上下左右內(nèi)邊距:
android:padding="5dp"
各自設(shè)置內(nèi)邊距:
android:paddingTop="5dp" android:paddingBottom="5dp" android:paddingLeft="5dp" android:paddingRight="5dp"
2.線性布局(Linear Layout)
LinearLayout 核心屬性:
? (1) android:orientation:兩個屬性值:“vertical” 垂直 “horizontal”水平
? (2) android:layout_weight 將父控件的剩余空間按照設(shè)置的權(quán)重比例再分配
2.1示例:
<LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1"> </Button> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2"> </Button> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button3"> </Button> </LinearLayout>
2.2微信界面實戰(zhàn)
<?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:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="×" android:textSize="50dp" android:layout_marginLeft="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="5dp" android:text="微信號/QQ/郵箱登錄" android:textColor="@color/black" android:textSize="30dp"/> <!--第一個框架--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="6dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="25dp" android:text="賬號" android:textColor="@color/black" android:textSize="25dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0"> <EditText android:layout_width="wrap_content" android:layout_height="match_parent" android:text="請?zhí)顚懳⑿盘?QQ號/郵箱 "/> </LinearLayout> </LinearLayout> <!--第二個框架--> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="10dp" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="25dp" android:text="密碼" android:textColor="@color/black" android:textSize="25dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請?zhí)顚懨艽a "/> </LinearLayout> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用手機號登錄" android:layout_marginTop="20dp" android:layout_marginLeft="25dp" android:textSize="20dp" android:textColor="@color/purple_500"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登錄" android:textSize="30dp" android:layout_marginTop="30dp" /> <!-- 第三個框架--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="找回密碼" android:layout_marginLeft="80dp" android:textColor="@color/purple_500" android:textSize="15dp" /> </LinearLayout> <LinearLayout android:layout_width="122dp" android:layout_height="wrap_content" android:layout_weight="7"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="緊急凍結(jié)" android:layout_marginLeft="40dp" android:textColor="@color/purple_500" android:textSize="15dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="70"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="微信安全中心" android:textColor="@color/purple_500"/> </LinearLayout> </LinearLayout> </LinearLayout>
3.總結(jié)
到此這篇關(guān)于Android基礎(chǔ)知識及線性布局介紹的文章就介紹到這了,更多相關(guān)Android線性布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Service中使用Toast無法正常顯示問題的解決方法
這篇文章主要介紹了Android Service中使用Toast無法正常顯示問題的解決方法,分析了Service中Toast無法正常顯示的原因與相關(guān)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下2016-10-10Kotlin StateFlow單數(shù)據(jù)更新熱流設(shè)計與使用介紹
StateFlow當(dāng)值發(fā)生變化,就會將值發(fā)送出去,下流就可以接收到新值。在某些場景下,StateFlow比LiveData更適用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09完美解決Android Studio集成crashlytics后無法編譯的問題
下面小編就為大家?guī)硪黄昝澜鉀QAndroid Studio集成crashlytics后無法編譯的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Android 后臺發(fā)送郵件示例 (收集應(yīng)用異常信息+Demo代碼)
今天介紹個更簡單的方法,我們把異常信息收集后,通過后臺發(fā)送郵件方法,把相關(guān)異常信息發(fā)送到我們指定的郵箱里面2013-07-07