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

300 人关注

我有一个 LinearLayout (水平方向),包含3个按钮。我希望这3个按钮有一个固定的宽度,并且均匀地分布在 LinearLayout 的宽度上。

我可以通过将 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>
    
4 个评论
这里有谁是来自谷歌用户界面课程的人吗 :3
你可以通过程序设置权重总和,并在for循环中制作按钮。 stackoverflow.com/questions/11611531/...
android
android-linearlayout
yamspog
yamspog
发布于 2010-08-13
24 个回答
Dan Dyer
Dan Dyer
发布于 2017-11-09
已采纳
0 人赞同

在此基础上扩展 fedj的回答 如果你将 layout_width 设置为 0dp ,并将每个按钮的 layout_weight 设置为1,那么可用的宽度将在各按钮之间平均分享。

扩展一下,如果你不想让按钮的宽度占到屏幕的1/3,可以把每个按钮包在一个LinearLayout中,并在3个LinearLayout中设置 layout_width="0dp" 和 layout_weight="1"。 此外,将LinearLayout的重力设置为 "中心",这样按钮就会在每个LinearLayout的中心位置对齐。
这只是线性布局的情况。谁能给相对布局中的等间距提供任何想法?
在我的案例中,我使用layout_width="wrap_content",并且只使用layout_weight="1",效果不错。
这是不正确的,因为OP想用固定的。请看 stoefln 的回答。
在imageview中,我们需要等量的padding,怎么办? 不能给imageview设置权重,因为图像会被拉伸。
stoefln
stoefln
发布于 2017-11-09
0 人赞同

如果你不想让按钮缩放,而是调整按钮之间的间距(所有按钮之间的间距相等),你可以使用views with 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>
    
这绝对是最简单、开销最小的方法(对于你想让视图之间的空间缩放,而不是视图本身的情况)。我是垂直使用的,所以只是交换了宽度和高度的值。谢谢。
这是一个优雅的解决方案。你可能更喜欢使用 Space 视图类型。让事情更有可读性。
瑞安,是的,那是如果你使用API 14以上(我们应该这样做)。
说实话,我以为有4个按钮就不能工作了,但它工作得很出色,没有任何大惊小怪,运行时间也没有测量。尊重!
这比目前被接受为答案的那个好。
Joop
Joop
发布于 2017-11-09
0 人赞同

You can do this by giving both View s a layout_width of 0dp and a layout_weight of 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>

安卓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_weights. 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.
  • 所以这就是为什么如果你给layout_width一个0dp的大小,第一步没有任何附加的意义,因为这两个视图没有被分配任何空间。那么只有第二点决定了每个View得到的空间,从而根据比例给View提供你指定的空间!

    解释为什么0dp会导致空间平分,提供一个显示相反的例子。下面的代码将导致不同的结果,因为example text现在有一个大于0dp的宽度,因为它有wrap_content,而使剩下的自由空间划分小于100%,因为文本需要空间。结果是,他们确实得到了50%的自由空间,但文本已经占用了一些空间,所以TextView将有well over 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>
        
    Gibolt
    Gibolt
    发布于 2017-11-09
    0 人赞同

    The modern solution for this is 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>
    

    请务必导入implementation 'com.google.android:flexbox:2.0.0'

    替换代码2】要强大得多;它是对ConstraintLayout的良好补充。这是一个很好的资源以了解更多信息。

    谢谢你让我(我们)知道了它。绝对有用。我使用了 "space_around",当我在FlexBox内有1或3个可见的按钮时,这些按钮很好地居中了。
    Sandhu
    Sandhu
    发布于 2017-11-09
    0 人赞同

    你可以像以下那样使用它。

    <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>
        
    这真是太棒了!!直到现在我才听说 Space
    fedj
    fedj
    发布于 2017-11-09
    0 人赞同

    你应该看一下android:layout_weight属性

    我的理解是,layout_weight属性将拉伸按钮的尺寸以填充布局。 我想保持按钮的大小不变,只是增加按钮之间的填充。
    fedj
    啊 !我真的不知道该怎么做。也许可以把每个按钮都包围在布局中。3个带有layout_weight属性的布局,并将按钮放在布局的中心。但我真的不知道这是否是最好的方法。
    marsbear
    marsbear
    发布于 2017-11-09
    0 人赞同

    好吧,如果你正好有3个按钮,而且如果可以(甚至计划)让外侧的按钮向左右两边对齐,那么你可能想试试相对布局,它的开销更少(在许多情况下)。

    你可以使用 layout_alignParentBottom 将所有按钮与布局的底部对齐。对外部按钮使用 layout_alignParentLeft and Right ,对中间按钮使用 layout_centerHorizontal

    这在不同的方向和屏幕尺寸上会有很好的效果。

    要说明的是,如果你有三个以上的按钮,这就不起作用。
    正是如此。你必须使用LinearLayout或RadioGroup(如果适用)来代替。
    Keshav
    Keshav
    发布于 2017-11-09
    0 人赞同

    你应该使用一个 android:weightSum 属性的线性布局。给线性布局一个相当于布局内按钮数量的weightSum,然后设置 android:layout_weight="1" 并设置按钮的宽度【替换代码2 此外,你可以使用paddings和layout margins来设计布局。

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

    为了动态地做到这一点

    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);
        
    我也希望它是动态的
    Stephen
    Stephen
    发布于 2017-11-09
    0 人赞同

    linearLayout 中不给 Button 本身加权,而是给 <Space> 设置权重。查看这不会拉伸 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>
    

    因为我使用的是4个<Space>。我设置了android:weightSum="4"。在linear layout中,这也是结果:。

    Muhammad
    Muhammad
    发布于 2017-11-09
    0 人赞同

    为了使两个按钮在水平线性布局中保持均匀的间距,我使用了3个LinearLayout对象作为空间,这些空间将被自动调整大小。我把这些LinearLayout对象定位如下。

    [] Button1 [] Button2 []

    ([]代表一个用于间隔的LinearLayout对象)

    然后我把这些[]LinearLayout对象的权重都设置为1,我就得到了间隔均匀的按钮。

    Hope this helps.

    martyglaubitz
    martyglaubitz
    发布于 2017-11-09
    0 人赞同

    我创建了一个自定义视图 分布式布局 to do this.

    matdev
    matdev
    发布于 2017-11-09
    0 人赞同

    我建议你使用LinearLayout的weightSum属性。

    替换代码0】到你的LinearLayout的xml声明中,然后将 android:layout_weight="1" 添加到你的按钮中,将导致3个按钮被均匀地分布。

    Jorgesys
    Jorgesys
    发布于 2017-11-09
    0 人赞同

    这可以通过以下方式实现:指定 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);
    

    所以我们可以在我们的设备中得到这个结果。

    即使我们旋转我们的设备weight在每个按钮中定义的元素可以沿着容器均匀地分布。

    0 人赞同

    最好的方法是使用 TableLayout android:layout_width="match_parent" ,在列中使用 android:layout_weight="1" ,用于所有列。

    sean le roy
    sean le roy
    发布于 2017-11-09
    0 人赞同

    上述使用layout_didn't work for me的答案,但下面的答案对我有用。

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

    Rajesh N
    Rajesh N
    发布于 2017-11-09
    0 人赞同

    以上的答案都是正确的,但如果你需要可见的和消失的功能,那么这个实用的方法会很有效。

    <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;
        
    Gibran Castillo
    Gibran Castillo
    发布于 2017-11-09
    0 人赞同

    同等权重的儿童

    要创建一个线性布局,其中每个孩子都使用相同数量的 空间,请将每个视图的android:layout_height设置为 "0dp"(用于垂直布局)或每个视图的android:layout_width 为 "0dp"(用于水平布局)。然后将每个视图的android:layout_weight 为 "1"。

    为了使其在 LinearLayout 视图组中工作, android:layout_width android:layout_height 的属性值需要等于 "match_parent" ...

    khiter med achraf
    khiter med achraf
    发布于 2017-11-09
    0 人赞同

    你可以使用这个。它是如此容易理解。 由 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>
        
    Dinesh Gowd
    Dinesh Gowd
    发布于 2017-11-09
    0 人赞同
    <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>
    

    在圆圈中,汤姆、蒂姆和托德被假定为3厘米。如果你想让它在屏幕上被触摸到,就把它写成汤姆和蒂姆被假定为1厘米,这意味着他们是虚拟的,但其二维平面是在底部。这将显示在屏幕上。

    Cesar Bobadilla
    Cesar Bobadilla
    发布于 2017-11-09
    0 人赞同

    最简单和最快的方法(但不是最好的)是添加一个空文本属性的TextView,像这样

    android:text=""
    

    在LinearLayout中,背景颜色必须是相同的,那么你可以使用padding属性,像这样

    android:paddingBottom="250dp"
    

    或任何你需要的东西。 下面是一个例子。

    Adrian Narducci
    Adrian Narducci
    发布于 2017-11-09
    0 人赞同

    如果你想让3个按钮有一个固定的宽度,并且均匀地分布在布局的宽度上......为什么不使用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>
        
    我想通过java,它是动态的,而不是通过xml。
    那么你可能需要一个Horizontal RecyclerView。 geeksforgeeks.org/… 或者,如果你需要一个具有不同组件的列表,只需做一个异质的recyclerView。 guides.codepath.com/android/...
    Saydiyev Oybek
    Saydiyev Oybek
    发布于 2017-11-09
    0 人赞同

    宽度和be均匀地分布在布局的宽度上。为什么不使用 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>```
        
    Kirill Häuptli
    Kirill Häuptli
    发布于 2017-11-09
    0 人赞同

    另一个解决方案是将按钮嵌套在一个约束布局内,并以这种方式使它们有相等的空间。请确保正确设置约束条件,这样按钮在布局中就会有相等的间距。

    即使你有一个LinearLayout作为根,也能工作。

    <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>
        
    basem
    basem
    发布于 2017-11-09
    0 人赞同
    <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"