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

Android RadioButton 等間距排列:使用 layout_weight

     編輯:程式設計綜合問答

XML 佈局示例

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

    <RadioButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="選項 1" />

    <RadioButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="選項 2" />

</RadioGroup>

原理

  • layout_width="0dp" + layout_weight="1" 表示「不佔固定寬度,按權重平分剩餘空間」。
  • 多個子視圖權重相同 → 等寬、等間距(視 RadioButton 自身 padding/drawable 而定)。

進階

若需固定間距而非平分,可在 RadioButton 外層包裹 LinearLayout 並設置 layout_margin,或使用 ConstraintLayout 鏈式佈局(app:layout_constraintHorizontal_chainStyle="spread_inside")。

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