【问题标题】:how to fit buttons size in Button Toggle Group如何在按钮切换组中调整按钮大小
【发布时间】:2021-11-03 22:06:47
【问题描述】:

我的设计包含一个可供选择的按钮组,例如:

我想实现这个按钮被拉伸到Button Toggle Group 的宽度,就像照片上的这里一样:

我尝试在 Button Toggle Group 中添加 ConstraintLayout,但没有成功,我没有任何想法。帮帮我,这里是代码:

<com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@+id/button_group"
            android:layout_width="380dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:gravity="center">

            <Button
                android:id="@+id/button_one"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bachelor" />

            <Button
                android:id="@+id/button_center"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Master" />

            <Button
                android:id="@+id/button_three"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Doctoral" />

        </com.google.android.material.button.MaterialButtonToggleGroup>

【问题讨论】:

    标签: java android android-layout button material-components


    【解决方案1】:

    您可以使用所有按钮上的android:layout_weight 属性和0dp 的宽度来应用权重来解决这个问题:

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

    对其余按钮应用相同的操作。

    之前:

    之后:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多