java - 如何在按钮切换组中调整按钮大小

标签 java android android-layout button material-components

我的设计包含一个按钮组,其中有选择,例如:
enter image description here
我希望实现此按钮被均匀拉伸(stretch)到 Button Toggle Group 的宽度,如照片所示:
enter image description here
我尝试在 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>

最佳答案

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

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

对其余按钮应用相同的方法。

之前:

enter image description here

之后:

enter image description here

关于java - 如何在按钮切换组中调整按钮大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69080941/

相关文章:

java - JTable 列标题本地化

java - 如何录制 PCM 数小时,但停止时仅保存最后 5 分钟的录音?

android - 我无法从我的应用程序中的包中获取数据

android - 约束布局 : How to constrain a View's top to the bottom of the view of largest height?

android - 为什么Android Studio 中没有\res\menu\main.xml 的Preview?

java - Spring 启动|本地主机 : 8080 404 error page displayed

java - 仅更新 hibernate 中允许的列

java - Android开发-HttpClient异常

android - 使用并行多个异步任务处理单个进度对话框

android - Android Studio 中项目的应用程序类在哪里?