简体   繁体   English

是否可以在 LinearLayout 的宽度上均匀分布按钮

[英]Is it possible to evenly distribute buttons across the width of a LinearLayout

I have a LinearLayout (oriented horizontally) that contains 3 buttons.我有一个包含 3 个按钮的LinearLayout (水平方向)。 I want the 3 buttons to have a fixed width and be evenly distributed across the width of the LinearLayout .我希望 3 个按钮具有固定宽度并均匀分布在LinearLayout的宽度上。

I can manage this by setting the gravity of the LinearLayout to center and then adjusting the padding of the buttons, but this works for a fixed width and won't work for different devices or orientations.我可以通过将LinearLayout的重力设置为中心然后调整按钮的填充来管理它,但这适用于固定宽度并且不适用于不同的设备或方向。

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

    <Button
        android:id="@+id/btnOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />

    <Button
        android:id="@+id/btnTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />


    <Button
        android:id="@+id/btnThree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120dp" />
</LinearLayout>

扩展fedj 的答案,如果您将layout_width设置为0dp并将每个按钮的layout_weight设置为 1,则可用宽度将在按钮之间平均共享。

If you don't want the buttons to scale, but adjust the spacing between the buttons (equal spacing between all buttons), you can use views with weight="1" which will fill the space between the buttons:如果您不希望按钮缩放,而是调整按钮之间的间距(所有按钮之间的间距相等),您可以使用 weight="1" 的视图,这将填充按钮之间的空间:

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

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@null"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/tars_active" />

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

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@null"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/videos_active" />

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

You can do this by giving both View sa layout_width of 0dp and a layout_weight of 1 :你可以通过给View sa layout_width 0dplayout_weight 1来做到这一点:

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

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

    <TextView
        android:layout_width="0dp"
        android:text="example text"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

The way android layout_weight works is that: android layout_weight 的工作方式是:

  • first, it looks to the size that a View would normally take and reserves this space.首先,它查看视图通常占用的大小并保留此空间。
  • second, if the layout is match_parent then it will divide the space that is left in the ratio of the layout_weight s.其次,如果布局是match_parent那么它将按照layout_weight的比例划分剩余空间 Thus if you gave the Views layout_weight="2" and layout_weight="1" ,the resultant ratio will be 2 to 1,that is : the first View will get 2/3 of the space that is left and the other view 1/3.因此,如果您给 Views layout_weight="2"layout_weight="1" ,结果比率将为 2 比 1,即:第一个 View 将获得剩余空间的 2/3,另一个 View 将获得 1/ 3.

So that's why if you give layout_width a size of 0dp the first step has no added meaning since both Views are not assigned any space.所以这就是为什么如果你给layout_width一个0dp的大小,第一步没有额外的意义,因为两个视图都没有分配任何空间。 Then only the second point decides the space each View gets, thus giving the View s the space you specified according to the ratio!那么只有第二个点决定了每个View获得的空间,从而给View s 根据比例指定的空间!

To explain why 0dp causes the space to devide equally by providing an example that shows the opposite: The code below would result in something different since example text now has a width that is greater than 0dp because it has wrap_content instead making the free space left to divide less than 100% because the text takes space.通过提供一个显示相反的示例来解释为什么0dp导致空间平均分配:下面的代码将导致不同的结果,因为example text现在具有大于0dp的宽度,因为它具有wrap_content而不是将可用空间留给划分小于 100%,因为文本占用空间。 The result will be that they do get 50% of the free space left but the text already took some space so the TextView will have well over 50% of the total space.结果将是他们确实获得了 50% 的可用空间,但文本已经占用了一些空间,因此TextView将拥有超过 50%的总空间。

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

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

    <TextView
        android:layout_width="wrap_content"
        android:text="example text"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

You may use it with like the following.你可以像下面这样使用它。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="15dp">
    <Space
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"/>
    <Space
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reset"/>
    <Space
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancel"/>
    <Space
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

The modern solution for this is Flexbox .对此的现代解决方案是Flexbox

<com.google.android.flexbox.FlexboxLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:justifyContent="space_around"> <!-- or "space_between", "space_evenly" -->

    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:width="120dp" />

    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:width="120dp" />

    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:width="120dp" />
</com.google.android.flexbox.FlexboxLayout>

Make sure to import implementation 'com.google.android:flexbox:2.0.0'确保导入implementation 'com.google.android:flexbox:2.0.0'

Flexbox is far more powerful; Flexbox功能Flexbox it is a good complement to ConstraintLayout .它是ConstraintLayout一个很好的补充。 This is a great resource to learn more.这是了解更多信息的绝佳资源

space_around、space_between 和 space_evenly 之间的区别

Well, if you have exactly 3 buttons and if it is ok (or even planned) that the outer buttons are aligned to the left and right side then you might want to try a RelativeLayout which is less overhead (in many situations).好吧,如果您正好有 3 个按钮,并且可以(甚至计划)将外部按钮与左侧和右侧对齐,那么您可能想尝试一个开销较小的 RelativeLayout(在许多情况下)。

You can use layout_alignParentBottom to align all buttons with the bottom of the layout.您可以使用layout_alignParentBottom将所有按钮与布局底部对齐。 Use layout_alignParentLeft and Right for the outer buttons and layout_centerHorizontal for the middle button.对外部按钮使用layout_alignParentLeft and Right ,对中间按钮使用layout_centerHorizontal

That will work well on different orientations and screen sizes.这将适用于不同的方向和屏幕尺寸。

你应该看看 android:layout_weight 属性

For evenly spacing out two buttons in a horizontal linear layout, I used 3 LinearLayout objects to act as spaces which are going to be automatically resized.为了在水平线性布局中均匀间隔两个按钮,我使用了 3 个 LinearLayout 对象作为将自动调整大小的空间。 I positioned these LinearLayout objects as follow:我将这些 LinearLayout 对象定位如下:

[] Button1 [] Button2 [] [] 按钮 1 [] 按钮 2 []

([] represents a LinearLayout object used for spacing) ([] 代表一个用于间距的 LinearLayout 对象)

then I set each of these [] LinearLayout objects' weights to 1, and I get evenly spaced out buttons.然后我将这些 [] LinearLayout 对象的权重设置为 1,并且我得到了均匀分布的按钮。

Hope this helps.希望这可以帮助。

我创建了一个自定义的 View DistributeLayout来做到这一点。

I suggest you use LinearLayout's weightSum attribute.我建议你使用 LinearLayout 的 weightSum 属性。

Adding the tag android:weightSum="3" to your LinearLayout's xml declaration and then android:layout_weight="1" to your Buttons will result in the 3 buttons being evenly distributed.将标签android:weightSum="3"到您的 LinearLayout 的 xml 声明中,然后将android:layout_weight="1"到您的按钮将导致 3 个按钮均匀分布。

This can be achieved assigning weight to every button added inside the container, very important to define horizontal orientation :这可以通过为容器内添加的每个按钮分配weight来实现,这对于定义水平方向非常重要:

    int buttons = 5;

    RadioGroup rgp = (RadioGroup) findViewById(R.id.radio_group);

    rgp.setOrientation(LinearLayout.HORIZONTAL);

    for (int i = 1; i <= buttons; i++) {
        RadioButton rbn = new RadioButton(this);         
        rbn.setId(1 + 1000);
        rbn.setText("RadioButton" + i);
        //Adding weight
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
        rbn.setLayoutParams(params);
        rgp.addView(rbn);
    }

so we can get this in our device as a result:所以我们可以在我们的设备中得到这个结果:

在此处输入图片说明

even if we rotate our device the weight defined in each button can distribuite the elemenents uniformally along the container:即使我们旋转设备,每个按钮中定义的weight也可以沿容器均匀分布元素:

在此处输入图片说明

最好的方法是将TableLayoutandroid:layout_width="match_parent"一起使用,并且在列中对所有列使用android:layout_weight="1"

The above answers using layout_didn't work for me, but the following did.使用 layout_ 的上述答案对我不起作用,但以下答案适用。

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="0.1"
    android:layout_gravity="center_horizontal"
    >

    <android.support.design.widget.FloatingActionButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
       />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"/>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"/>

</LinearLayout>

This is how it looks on screen,这是它在屏幕上的样子,

在此处输入图片说明

Above all answers are right but In a case you need visible and gone features then this pragmatically method will work well最重要的是答案是正确的,但如果您需要可见和消失的功能,那么这种务实的方法会很好用

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

        <Button
            android:id="@+id/btnOne"
            android:layout_width="120dp"
            android:layout_height="match_parent"></Button>

        <Button
            android:id="@+id/btnTwo"
            android:layout_width="120dp"
            android:layout_height="match_parent"></Button>


        <Button
            android:id="@+id/btnThree"
            android:layout_width="120dp"
            android:layout_height="match_parent"></Button>
    </LinearLayout>



 float width=CommonUtills.getScreenWidth(activity);
            int cardWidth=(int)CommonUtills.convertDpToPixel (((width)/3),activity);

LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(width,
                        LinearLayout.LayoutParams.MATCH_PARENT);

btnOne.setLayoutParams(params);
btnTwo.setLayoutParams(params);
btnThree.setLayoutParams(params);

public class CommonUtills {
public static float getScreenWidth(Context context) {
        float width = (float) 360.0;
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        width = displayMetrics.widthPixels / displayMetrics.density;
        return width;
    }
}

Equally weighted children 体重相同的孩子

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout).要创建一个线性布局,其中每个孩子在屏幕上使用相同的空间量,请将每个视图的 android:layout_height 设置为“0dp”(对于垂直布局)或每个视图的 android:layout_width 设置为“0dp”(用于水平布局)。 Then set the android:layout_weight of each view to "1".然后将每个视图的 android:layout_weight 设置为“1”。

In order for this to work in the LinearLayout view group the attribute values for android:layout_width and android:layout_height need to be equal to "match_parent" ...为了使其在LinearLayout视图组中工作, android:layout_widthandroid:layout_height的属性值需要等于"match_parent" ...

You should use an android:weightSum attribute linear layout.您应该使用android:weightSum属性线性布局。 Give linear layout a weightSum equal to the number of Buttons inside the layout, then set android:layout_weight="1" and set width of the button android:layout_width="0dp" further, you can style the layout using paddings and layout margins.给线性布局一个等于布局内按钮数量的 weightSum,然后设置android:layout_weight="1"并设置按钮的宽度android:layout_width="0dp"进一步,您可以使用填充和布局边距来设置布局样式。

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:weightSum="3">
    
    <Button
        android:id="@+id/btnOne"
        android:layout_width="0dp"
        android:text="1"
        android:layout_height="wrap_content"
        android:width="120dip"
        android:layout_weight="1"
        android:layout_margin="15dp" 
        />
    
    <Button
        android:id="@+id/btnTwo"
        android:text="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:width="120dip"
        android:layout_weight="1"
        android:layout_margin="15dp" />

    <Button
        android:id="@+id/btnThree"
        android:text="3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:width="120dip"
        android:layout_weight="1"
        android:layout_margin="15dp" />

</LinearLayout>

In order to do it dynamically为了动态地做

void initiate(Context context){
    LinearLayout parent = new LinearLayout(context);

    parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    parent.setWeightSum(3);
    parent.setOrientation(LinearLayout.HORIZONTAL);

    AppCompatButton button1 = new AppCompatButton(context);
    button1.setLayoutParams(new LinearLayout.LayoutParams(0 ,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f));

    AppCompatButton button2 = new AppCompatButton(context);
    button2.setLayoutParams(new LinearLayout.LayoutParams(0 ,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f));

    AppCompatButton button3 = new AppCompatButton(context);
    button3.setLayoutParams(new LinearLayout.LayoutParams(0 ,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f));
    
    parent.addView(button1);
    parent.addView(button2);
    parent.addView(button3);

}

In linearLayout Instead of giving weight to Button itself , set the weight to <Space> View this won't stretch the Button .linearLayout不是给Button本身赋予权重,而是将权重设置为<Space> View 这不会拉伸Button

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.955">

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

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:background="@drawable/ic_baseline_arrow_back_24" />

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

        <Button
            android:id="@+id/captureButton"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:background="@drawable/ic_round_camera_24" />

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

        <Button
            android:id="@+id/cameraChangerBtn"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:background="@drawable/ic_round_switch_camera_24" />

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

    </androidx.constraintlayout.widget.ConstraintLayout>

as I am using 4 <Space> I set the android:weightSum="4" In linear layout also this is the result ::因为我使用 4 <Space>我设置了android:weightSum="4" linear layout这也是结果 ::

布局预览

you can use this .你可以用这个。 it's so easy to understand : by https://developer.android很容易理解: https : //developer.android

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
    android:text="Tom"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="24sp" />

<TextView
    android:text="Tim"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="24sp" />

<TextView
    android:text="Todd"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="24sp" />

</LinearLayout>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Tom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="24sp" 
        android:layout_weight="3"/>

    <TextView
        android:text="Tim"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="24sp"
        android:layout_weight="3"/>

    <TextView
        android:text="Todd"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="24sp" 
        android:layout_weight="3"/>

</LinearLayout>

In circle, Tom, Tim, and Todd get assumed to be 3 centimeters.在圆圈中,汤姆、蒂姆和托德被假定为 3 厘米。 If you want it to be touch down screen, put it as Tom and Tim getting assumed to be 1 centimeter, which means they combine virtual but its 2D plane is at the bottom.如果你想让它成为触屏,把它当作汤姆和蒂姆假设为 1 厘米,这意味着他们结合了虚拟但它的 2D 平面在底部。 This is displayed on screen.这显示在屏幕上。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
    android:text="Tom"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:textSize="24sp" />

<TextView
    android:text="Tim"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:textSize="24sp" />

<TextView
    android:text="Todd"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:textSize="24sp" />

The easiest and fastest way, (but not the best), is to add a TextView with empty text attribute, like this最简单和最快的方法(但不是最好的)是添加一个带有空文本属性的 TextView,就像这样

android:text=""

the background color must be the same at the LinearLayout, then you could use the padding property, like this LinearLayout 的背景颜色必须相同,然后您可以使用 padding 属性,如下所示

android:paddingBottom="250dp"

or whatever you need.或任何你需要的。 Here is an example.这是一个例子。

If you want the 3 buttons to have a fixed width and be evenly distributed across the width of the layout... why not use constraintLayout?如果您希望 3 个按钮具有固定宽度并均匀分布在布局的宽度上...为什么不使用约束布局?

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <Button
            android:id="@+id/btnOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="120dip"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/btnTwo">
            
        </Button>

        <Button
            android:id="@+id/btnTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="120dip"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/btnThree"
            app:layout_constraintStart_toEndOf="@id/btnOne"></Button>


        <Button
            android:id="@+id/btnThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="120dip"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/btnTwo"
            app:layout_constraintEnd_toEndOf="parent">
            
        </Button>

</androidx.constraintlayout.widget.ConstraintLayout>

Width and be are evenly distributed across the width of the layout. width 和 be 在布局的宽度上均匀分布。 Why not use constraintLayout?为什么不使用 constraintLayout?

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">

 <Button android:id="@+id/btnOne" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:width="120dip"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintEnd_toStartOf="@id/btnTwo">
 </Button> 

 <Button android:id="@+id/btnTwo" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:width="120dip" 
  app:layout_constraintTop_toTopOf="parent" 
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintEnd_toStartOf="@id/btnThree"
  app:layout_constraintStart_toEndOf="@id/btnOne"></Button> <Button 
  android:id="@+id/btnThree" android:layout_width="wrap_content"    
  android:layout_height="wrap_content" android:width="120dip" 
  app:layout_constraintTop_toTopOf="parent" 
  app:layout_constraintBottom_toBottomOf="parent" 
  app:layout_constraintStart_toEndOf="@id/btnTwo" 
  app:layout_constraintEnd_toEndOf="parent"> 
 </Button>

</androidx.constraintlayout.widget.ConstraintLayout>```

Another solution would be to nest buttons inside a constraint layout and space them equaly this way.另一种解决方案是将按钮嵌套在约束布局内,并以这种方式将它们均匀分布。 Make sure to set the constraints properly, so the buttons will be spaced equaly in the layout.确保正确设置约束,以便按钮在布局中均匀分布。

Works even if you have a LinearLayout as root.即使你有一个 LinearLayout 作为 root 也能工作。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/deleteButton"
        android:layout_width="wrap_content"
        android:layout_height="64dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="asdf"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/shareButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/shareButton"
        android:layout_width="wrap_content"
        android:layout_height="64dp"

        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="sdsfa"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/deleteButton"
        app:layout_constraintTop_toTopOf="@+id/deleteButton" />
</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM