简体   繁体   English

使 2 个具有可变内容的按钮大小相同 Android

[英]Make 2 buttons with variable content the same size Android

I am trying in a constraint layout to make two buttons take the same size as the one with more text.我正在尝试在约束布局中使两个按钮的大小与具有更多文本的按钮的大小相同。

This is what I have这就是我所拥有的在此处输入图像描述

This is what I want这就是我要的在此处输入图像描述

Playing with the height attribute of the buttons, setting it to "0" you can get one button to follow the other, but the other does not do the same使用按钮的高度属性,将其设置为“0”,您可以让一个按钮跟随另一个按钮,但另一个按钮不这样做

here is the code这是代码

<androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/tools_ly_first_buttons"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin_bubble_answer_buttons"
                    android:layout_marginEnd="@dimen/margin_bubble_answer_buttons"
                    android:layout_marginBottom="2dp"
                    android:visibility="visible"
                    app:layout_constraintEnd_toEndOf="@+id/tools_btn_1"
                    app:layout_constraintStart_toEndOf="@+id/tools_btn_1"
                    tools:visibility="visible">

                    <Button
                        android:id="@+id/tools_btn_1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="@drawable/btn_round_confirmation_yes"
                        android:fontFamily="@font/calibri_regular"
                        android:textAllCaps="false"
                        android:textColor="@color/white"
                        android:layout_marginEnd="1dp"
                        android:textSize="14sp"
                        android:padding="@dimen/padding_bubble_answer_buttons_text"
                        tools:text="Perfect asdasdas."
                        app:layout_constraintEnd_toStartOf="@+id/tools_btn_2"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="@id/tools_ly_first_buttons"
                        app:layout_constraintBottom_toBottomOf="@id/tools_ly_first_buttons"/>

                    <Button
                        android:id="@+id/tools_btn_2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:background="@drawable/btn_round_confirmation_yes"
                        android:fontFamily="@font/calibri_regular"
                        android:textAllCaps="false"
                        android:textColor="@color/white"
                        android:padding="@dimen/padding_bubble_answer_buttons_text"
                        android:layout_marginStart="1dp"
                        android:textSize="14sp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toEndOf="@+id/tools_btn_1"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        tools:text="Perfect Perfect square trition. Perfect Perfect square trition. Perfect Perfect square trition. Perfect Perfect square trition. Perfect Perfect square trition. Perfect Perfect square trition." />

                </androidx.constraintlayout.widget.ConstraintLayout>

You can do this by wrapping the two buttons into a LinearLayout (or another ConstraintLayout ) and setting their heights both to match_parent while setting the height of the container view to wrap_content .您可以通过将两个按钮包装到一个LinearLayout (或另一个ConstraintLayout )并将它们的高度都设置为match_parent ,同时将容器视图的高度设置为wrap_content来做到这一点。

If your ConstraintLayout is already a container to be nested in other views, than the extra nesting is not necessary.如果您的ConstraintLayout已经是要嵌套在其他视图中的容器,则不需要额外的嵌套。 Just set the button heights to match_parent and they will fill the container.只需将按钮高度设置为match_parent ,它们就会填满容器。

<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:id="@+id/button_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/btnLeft"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="Short Text"
            android:layout_weight="1.0"
            android:layout_margin="4dp"
            />

        <Button
            android:id="@+id/btnRight"
            android:text="Long Text Taking Up Many Many Many Lines of Space and More than One Line Definitely"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:layout_margin="4dp"
            />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Here's what this produces - doesn't matter which button has the long text.这就是它产生的结果 - 哪个按钮有长文本无关紧要。

示例布局

Note In the XML you shared, the parent ConstraintLayout appears to be constrained to one of its child views ( tools_btn_1 ).注意在您共享的 XML 中,父ConstraintLayout似乎被限制到其子视图之一 ( tools_btn_1 )。 This is probably not valid.这可能是无效的。

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

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