<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Android 線性布局示例 > 原文: [https://javatutorial.net/android-linear-layout-example](https://javatutorial.net/android-linear-layout-example) 在先前的教程中,您了解了 Android [意圖](https://javatutorial.net/android-intent-example),[服務](https://javatutorial.net/android-service-example)和[活動](https://javatutorial.net/android-activity-example)。 本教程和接下來的幾本教程將介紹有關 Android 中不同布局的信息。 布局是事物以特定方式進行排列的方式。 Android 還提供了不同的布局,以不同的方式排列不同的組件。 其中一些是線性布局,相對布局,Web 視圖布局等。布局對于 UI 應用程序的開發非常重要。 本教程的主要重點是線性布局。 ## 線性布局 Android 中的線性布局使我們可以將組件水平排列在單列中或垂直排列在單行中。 垂直或水平方向取決于屬性`android:orientation`。線性布局簡單易用,如果窗口的長度超過屏幕的長度,則會創建滾動條。 垂直線性布局每行只有一項。 線性布局具有許多不同的屬性,可用于根據需要自定義線性布局。 下圖顯示了水平和垂直線性布局。 ![Linear layout](https://img.kancloud.cn/31/bd/31bdb0713589b0b8ce3b2748a22bb381_854x714.jpg) 線性布局 ## 線性布局的屬性 以下是 Android 線性布局的一些屬性。 * `Id`:布局的唯一標識符。 * `Orientation`:用于將線性布局方向設置為垂直或水平的屬性。 * `Layout_Weight`:此屬性在每個組件處分配“重要性”值。 * `Gravity`:此屬性顯示對象在 x-y 平面中的位置,例如中心,右側,頂部,底部和左側。 * `Weight_sum`:此屬性定義最大加權和。 * `Divider`:繪圖可用作按鈕之間的垂直分隔線。 除了這些屬性,線性布局還具有許多不同的構造函數。 ## 線性布局的構造函數 以下是線性布局的構造函數 * `LinearLayout(Context context)` * `LinearLayout(Context context, AttributeSet attribute)` * `LinearLayout(Context context, AttributeSet attrs, int styleAttribute)` * `LinearLayout(Context context, AttributeSet attrs, int styleAttribute, int styleRes)` ## Android 中的線性布局示例 如果為兩個線性布局(例如垂直線性布局或水平線性布局)設置了不同的屬性值,則線性布局看起來會有所不同。 以下示例顯示垂直線性布局。 這是`activity_linear_vertical.xml` ```java <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="368dp" android:layout_height="495dp" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button2" /> <Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button3" /> <Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button4" /> </LinearLayout> ``` 這是輸出的樣子 ![linear layout Vertical](https://img.kancloud.cn/d4/42/d442b7144059a834531756ece0104c81_218x364.jpg) 垂直線性布局 這是另一個顯示水平線性布局的示例。 以下是`activity_linear_horizo??ntal.xml`的代碼 ```java <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="368dp" android:layout_height="495dp" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button4" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button3" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button2" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button1" /> </LinearLayout> ``` 這是輸出的樣子 ![linear Layout Horizontal](https://img.kancloud.cn/4e/b6/4eb683422ac0a75e308adcdee08e7cf6_255x426.jpg) 水平線性布局 您可以從[鏈接](https://github.com/JavaTutorialNetwork/Tutorials/blob/master/LinearLayoutExample.rar)下載示例代碼。 有關更多 Android 布局,請遵循以下教程。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看