简体   繁体   English

如何使用LinearLayout均匀分布圆形的ImageButton

[英]How to evenly space round ImageButtons with LinearLayout

I have found how to create round ImageButtons using a Shape background and I have found how to evenly space buttons in a LinearLayout by setting the width to 0dp and the weight to 1. But I can't combine both, so either the round buttons are not evenly spaced 我已经找到了如何使用Shape背景创建圆形ImageButton的方法,并且已经找到了如何通过将width设置为0dp并将权重设置为1来在LinearLayout中均匀间隔按钮的方法。但是我无法将两者组合在一起,所以圆形按钮都是间距不均匀
圆形但间隔不均匀

or they are evenly spaced but not round 或者它们间隔均匀但不圆
在此处输入图片说明

It seems the extra space is going to the inside button instead of to the margins. 似乎多余的空间将移至内部按钮而不是边缘。 How can I make a LinearLayout allocate the new empty space to margins and not to the view? 如何使LinearLayout将新的空白空间分配给边距而不是视图?

This is the layout that gives me the second image above 这是给我第二张图片的布局

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

And this is the background 这是背景

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

A solution I found was to use empty Space s around and in between the buttons and have those take the extra space 我发现的一个解决方案是在按钮之间和按钮之间使用空白Space ,并让它们占用额外的空间

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

I had to remove the weight from the actual buttons and set the width to wrap_content 我必须从实际按钮中删除权重,并将宽度设置为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"/>

Try this: 尝试这个:

Step 1: Set the android:layout_width="0dp" Step 2: Set the android:layout_weight equally among all button with sum equal to 1.0 步骤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