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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Android 相對布局示例 > 原文: [https://javatutorial.net/android-relative-layout-example](https://javatutorial.net/android-relative-layout-example) 本教程通過示例說明了 Android 中的相對布局。 Android 中的布局對于基于 GUI 的應用程序非常重要。 在上一教程中,我們學習了[線性布局](https://javatutorial.net/android-linear-layout-example),本教程介紹了 Android 中的相對布局。 ## 相對布局 顧名思義,相對布局顯示組件之間的相對位置。 可以相對于連續元素或父組件來指定位置。 相對布局是 Android 提供的最靈活的布局。 它使您可以在屏幕上放置元素。 默認情況下,它將所有組件設置在布局的左上方。 下圖顯示了相對布局的外觀, ![relative layout](https://img.kancloud.cn/be/1d/be1d1853b5cc6dd4a22c735b585e35e2_258x380.jpg) 相對布局 ## 相對布局屬性 以下是相對布局的屬性。 * `Id`:定義布局 ID * `Gravity`*它指定對象在 x-y 平面中的位置。 * `IgnoreGravity`:被添加以忽略特定組件上的重力。 ## 相對布局構造函數 相對布局具有四個不同的構造函數 * `RelativeLayout(Contetxt context)` * `RelativeLayout(Contetxt context, AttributeSet attribute)` * `RelativeLayout(Contetxt context, AttributeSet attribute, int defStyleAttribute)` * `RelativeLayout(Contetxt context, AttributeSet attribute, int defStyleAttribute, in defStyleRes)` ## 相對布局的方法 以下是相對布局的幾種重要方法 * `setGravity()`:它將子視圖的重力設置為居中,向左或向右。 * `setHorizo??ntalGravity()`:用于水平定位元素。 * `setVerticalGravity()`:用于垂直放置元素。 * `requestLayout()`:用于請求布局。 * `setIgnoreGravity()`:用于忽略任何特定元素的重力。 * `getGravity()`:用于獲取元素的位置。 * `getAccessibilityClassName()`:返回對象的類名稱。 ## 相對布局的 XML 屬性 相對布局具有以下 XML 屬性。 `android:layout_above`,它將給定組件的底部邊緣定位在給定組件 ID 上方。 `android:layout_alignBaseline`,它將給定組件的基線置于給定組件 ID 的基線之上。 `android:layout_alignBottom`,它在給定組件 ID 的底部對齊。 `android:layout_alignEnd`,它在給定組件 ID 的末尾對齊。 `android:layout_alignLeft`,它將組件定位在給定組件 ID 的左側。 `android:layout_alignRight`,將該組件定位在給定組件 ID 的右側。 ## 相對布局示例 以下示例顯示了 Android 中的相對布局。 創建一個空的活動并將相對布局從調色板拖到屏幕上。 然后拖動所需的 GUI 組件。 這是 xml 文件。 ```java <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="368dp" android:layout_height="495dp" xmlns:tools="http://schemas.android.com/tools" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="141dp" android:layout_marginStart="141dp" android:layout_marginTop="89dp" android:text="Sign In" android:textColor="@android:color/black" android:textColorLink="@android:color/black" android:textSize="28sp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="43dp" android:text="ID" android:textColor="@android:color/black" android:textSize="18sp" android:typeface="normal" android:layout_below="@+id/textView" android:layout_alignLeft="@+id/textView5" android:layout_alignStart="@+id/textView5" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" /> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Enter ID" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_alignLeft="@+id/editText2" android:layout_alignStart="@+id/editText2" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@android:color/black" android:text="Password" android:layout_alignBottom="@+id/editText2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="32dp" android:layout_marginStart="32dp" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_below="@+id/editText" android:ems="10" android:text="Password" android:inputType="textPassword" /> <Button android:id="@+id/S" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_below="@+id/editText2" android:layout_marginTop="32dp" android:text="SignIn" /> </RelativeLayout> ``` 這是輸出的樣子 ![relative layout example](https://img.kancloud.cn/33/6f/336f3ce45eb30f11dd4e97c86c116d04_180x300.jpg) 相對布局示例 您可以從[鏈接](https://github.com/JavaTutorialNetwork/Tutorials/blob/master/RelativeLayout.rar)下載源代碼。
                  <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>

                              哎呀哎呀视频在线观看