首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android RelativeLayout 實現兩個按鈕左右對齊緊湊排列

     編輯:程式設計綜合問答

XML 佈局

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/buttonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        android:layout_alignParentLeft="true" />

    <Button
        android:id="@+id/buttonShare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:layout_alignParentRight="true" />

</RelativeLayout>

關鍵屬性

  • layout_alignParentLeft="true":左按鈕貼左邊。
  • layout_alignParentRight="true":右按鈕貼右邊。
  • 若需兩按鈕**中間無間隙緊貼**,改用 LinearLayout + layout_weight="1" 平均分配,或 layout_toLeftOf="@id/buttonShare" 使左按鈕右邊對齊右按鈕左邊。

緊貼版 LinearLayout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/buttonClick"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Click" />

    <Button
        android:id="@+id/buttonShare"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Share" />

</LinearLayout>

兩按鈕各佔 50% 寬度,緊密相連無間隙。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved