簡體   English   中英

如何使用LinearLayout均勻分布圓形的ImageButton

[英]How to evenly space round ImageButtons with LinearLayout

我已經找到了如何使用Shape背景創建圓形ImageButton的方法,並且已經找到了如何通過將width設置為0dp並將權重設置為1來在LinearLayout中均勻間隔按鈕的方法。但是我無法將兩者組合在一起,所以圓形按鈕都是間距不均勻
圓形但間隔不均勻

或者它們間隔均勻但不圓
在此處輸入圖片說明

似乎多余的空間將移至內部按鈕而不是邊緣。 如何使LinearLayout將新的空白空間分配給邊距而不是視圖?

這是給我第二張圖片的布局

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

    <ImageButton
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_add_black_24dp"
            android:background="@drawable/rounded"/>

    <ImageButton
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_add_black_24dp"
            android:background="@drawable/rounded"/>

    <ImageButton
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_add_black_24dp"
            android:background="@drawable/rounded"/>
</LinearLayout>

這是背景

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
  <solid android:color="#33DDFF" />
  <corners android:radius="4dp"/>
</shape>

我發現的一個解決方案是在按鈕之間和按鈕之間使用空白Space ,並讓它們占用額外的空間

    <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" >
    </Space>

我必須從實際按鈕中刪除權重,並將寬度設置為wrap_content

 <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_add_black_24dp"

            android:background="@drawable/rounded"
            android:contentDescription="@string/speak_button_desc"/>

嘗試這個:

步驟1:將android:layout_width =“ 0dp”設置為步驟2:在所有按鈕中將android:layout_weight均等設置為總和等於1.0

<LinearLayout android:id="@+id/linearLayout3"
android:layout_height="wrap_content" 
android:orientation="horizontal"
android:layout_width="match_parent">

<ImageButton 
  android:layout_height="wrap_content"
  android:id="@+id/clear"
  android:layout_width="0dp"
  android:layout_weight=".5" />

 <ImageButton 
 android:layout_height="wrap_content"
 android:id="@+id/submit" 
 android:layout_width="0dp"
 android:layout_weight=".5" />
 </LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM