溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android基礎(chǔ)知識及線性布局的示例分析

發(fā)布時間:2022-01-14 11:23:02 來源:億速云 閱讀:125 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹Android基礎(chǔ)知識及線性布局的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

    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>

    效果如圖:

    Android基礎(chǔ)知識及線性布局的示例分析

    例子:

    <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>

    效果如圖:

    Android基礎(chǔ)知識及線性布局的示例分析

    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示例:

    Android基礎(chǔ)知識及線性布局的示例分析

    <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)

    Android基礎(chǔ)知識及線性布局的示例分析

    <?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="用手機(jī)號登錄"
                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>

    以上是“Android基礎(chǔ)知識及線性布局的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI