如何使用分段按钮在片段之间切换?

问题描述 投票:0回答:1

我需要得到以下设计。

  • 我需要一种不使用 TabLayout/ViewPager 的方法。
  • 在片段之间切换时无需滑动。

您能否帮助理解如何使用分段按钮进行设计?

android android-button
1个回答
0
投票

在您的布局中。

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/toggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        style="?attr/materialButtonOutlinedStyle"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
    />
    <Button
        style="?attr/materialButtonOutlinedStyle"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
    />
    <Button
        style="?attr/materialButtonOutlinedStyle"
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
    />
</com.google.android.material.button.MaterialButtonToggleGroup>

代码中:

toggleButton.addOnButtonCheckedListener { toggleButton, checkedId, isChecked ->
    // Respond to button selection
}

参考:https://github.com/material-components/material-components-android/blob/master/docs/components/Button.md#toggle-button

© www.soinside.com 2019 - 2024. All rights reserved.