You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何覆盖MaterialButtonToggleGroup的间距移除逻辑,为按钮添加间距?

How to Add Spacing Between Buttons in MaterialButtonToggleGroup

Great question! The MaterialButtonToggleGroup does default to collapsing all spacing between buttons for a tight, grouped look, but there are a couple of clean, official ways to override this behavior and add the spacing you need.

1. Use the Built-in buttonSpacing Attribute (Simplest Method)

The MaterialButtonToggleGroup has a dedicated attribute app:buttonSpacing that lets you set uniform spacing between all buttons in the group. This is the easiest and most recommended approach because it handles spacing consistently without extra margin hacks, and avoids unwanted spacing at the edges of the group.

Example XML:

<com.google.android.material.button.MaterialButtonToggleGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:buttonSpacing="8dp" <!-- Adjust this value to your desired spacing -->
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 1" />

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 2" />

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Option 3" />
</com.google.android.material.button.MaterialButtonToggleGroup>

Dynamic Adjustment in Code:

If you need to change the spacing at runtime, use the setButtonSpacing() method. Remember to convert dp to pixels for proper scaling:

Kotlin:

val desiredSpacingDp = 8
val spacingInPx = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    desiredSpacingDp.toFloat(),
    resources.displayMetrics
).toInt()
yourToggleGroup.setButtonSpacing(spacingInPx)

Java:

int desiredSpacingDp = 8;
int spacingInPx = (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    desiredSpacingDp,
    getResources().getDisplayMetrics()
);
yourToggleGroup.setButtonSpacing(spacingInPx);

2. Fine-Tune with Individual Button Margins

If you need non-uniform spacing (e.g., more space between specific buttons), you can add android:layout_margin attributes directly to individual MaterialButton components. Just be mindful to avoid extra spacing at the start/end of the group by adjusting margins selectively.

Example XML:

<com.google.android.material.button.MaterialButtonToggleGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First"
        android:layout_marginEnd="6dp" /> <!-- Add space to the right of the first button -->

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        android:layout_marginHorizontal="4dp" /> <!-- Space on both sides -->

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Third"
        android:layout_marginStart="6dp" /> <!-- Add space to the left of the third button -->
</com.google.android.material.button.MaterialButtonToggleGroup>

Notes:

  • For vertical toggle groups, app:buttonSpacing will automatically apply vertical spacing between buttons, no extra changes needed.
  • Make sure you're using a recent version of the Material Components library to access the buttonSpacing attribute (it's available in versions 1.2.0 and above).

Hope these solutions help you get the exact spacing you need!

内容的提问来源于stack exchange,提问作者Hondus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:49:06