Android基礎(chǔ)知識(shí)及線性布局介紹
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)容在這個(gè)view中的位置】
android:layout_gravity :【這個(gè)view相對(duì)于它父view的位置】
1、gravity在線性布局中不起任何作用,layout_gravity在線性布局中起作用;
2、 當(dāng)我們使用 android:orientation=“vertical” 時(shí), android:layout_gravity只有水平方向的設(shè)置才起作用,
垂直方向的設(shè)置不起作用。即:left,right,center_horizontal 是生效的;
3、當(dāng) 我們使用android:orientation=“horizontal” 時(shí), android:layout_gravity只有垂直方向的設(shè)置才起作用,
水平方向的設(shè)置不起作用。即:top,bottom,center_vertical 是生效的。
1.1控件的可見性
該屬性有三種狀態(tài)值:gone、visible、invisible。
gone 與invisible的區(qū)別是:
gone 表示控件不可見,也不會(huì)占任何的位置,也不會(huì)有任何響應(yīng)。
而invisible表示控件雖然不可見,但是會(huì)占據(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表示控件雖然不可見,但是會(huì)占據(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 表示控件不可見,也不會(huì)占任何的位置,也不會(huì)有任何響應(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的都會(huì)知道CSS里的盒模式有個(gè)外邊距和內(nèi)邊距。
外邊距可以設(shè)置視圖距離父視圖上下左右的距離。
內(nèi)邊距可以設(shè)置視圖內(nèi)部?jī)?nèi)容距離自己邊框上下左右的距離。
Android 的控件布局其實(shí)也用的是這個(gè)盒模式。
如果距離父視圖上下左右的外邊距相同,可以這么設(shè)置:
android:layout_margin="10dp"
我們也可以單獨(dú)的設(shè)置某個(gè)外邊距:
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:兩個(gè)屬性值:“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微信界面實(shí)戰(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="微信號(hào)/QQ/郵箱登錄"
android:textColor="@color/black"
android:textSize="30dp"/>
<!--第一個(gè)框架-->
<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="賬號(hào)"
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="請(qǐng)?zhí)顚懳⑿盘?hào)/QQ號(hào)/郵箱 "/>
</LinearLayout>
</LinearLayout>
<!--第二個(gè)框架-->
<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="請(qǐng)?zhí)顚懨艽a "/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用手機(jī)號(hào)登錄"
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"
/>
<!-- 第三個(gè)框架-->
<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ǔ)知識(shí)及線性布局介紹的文章就介紹到這了,更多相關(guān)Android線性布局內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android通過反射實(shí)現(xiàn)強(qiáng)制停止應(yīng)用程序的方法
這篇文章主要介紹了Android通過反射實(shí)現(xiàn)強(qiáng)制停止應(yīng)用程序的方法,涉及Android的反射機(jī)制與進(jìn)程操作的相關(guān)技巧,需要的朋友可以參考下2016-02-02
Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用
Android 官方提供了三個(gè)用來優(yōu)化布局的標(biāo)簽,分別是include、merge與ViewStub,下面這篇文章主要給大家介紹了關(guān)于Android布局技巧之include、merge與ViewStub標(biāo)簽巧用的相關(guān)資料,需要的朋友可以參考下2018-06-06
Android實(shí)現(xiàn)獲取聯(lián)系人電話號(hào)碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)獲取聯(lián)系人電話號(hào)碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android內(nèi)容提供者ContentProvider用法實(shí)例分析
這篇文章主要介紹了Android內(nèi)容提供者ContentProvider用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了內(nèi)容提供者ContentProvider獲取及解析數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android開發(fā)歡迎頁(yè)點(diǎn)擊跳過倒計(jì)時(shí)進(jìn)入主頁(yè)
沒點(diǎn)擊跳過自然進(jìn)入主頁(yè),點(diǎn)擊跳過之后立即進(jìn)入主頁(yè),這個(gè)功能怎么實(shí)現(xiàn)呢,本文通過實(shí)例代碼給大家介紹Android開發(fā)歡迎頁(yè)點(diǎn)擊跳過倒計(jì)時(shí)進(jìn)入主頁(yè),感興趣的朋友一起看看吧2023-12-12
android實(shí)現(xiàn)加載動(dòng)畫對(duì)話框
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)加載動(dòng)畫對(duì)話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼,準(zhǔn)確獲取周邊地理位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
Android教你如何發(fā)現(xiàn)APP卡頓的實(shí)現(xiàn)
這篇文章主要介紹了Android教你如何發(fā)現(xiàn)APP卡頓的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

