SegmentedButton:打造炫酷的Android分段按钮控件

SegmentedButton:打造炫酷的Android分段按钮控件

项目介绍

SegmentedButton 是一个开源的 Android 控件,旨在模仿 iOS 的 SegmentedControl 功能。这个项目最初是从 ceryle/SegmentedButton 分支出来的,但经过彻底的改造和优化,已经成为一个功能更加强大、灵活性更高的控件。

项目技术分析

SegmentedButton 提供了丰富的自定义选项,包括文本颜色、大小、字体、按钮图标、分隔线样式等。此外,它还支持实线和虚线边框、按钮点击时的涟漪效果、可拖动的按钮以及各种动画效果。最重要的是,SegmentedButton 支持从 API 16 开始的 Android 版本,确保了广泛的兼容性。

项目及技术应用场景

SegmentedButton 适用于各种需要用户选择的场景,例如:

  • 表单选择:在表单中提供多个选项供用户选择。
  • 导航切换:在应用的不同功能模块之间进行切换。
  • 设置选项:在设置界面中提供多个选项供用户配置。
  • 筛选条件:在筛选功能中提供多个条件供用户选择。

项目特点

  1. 高度自定义:支持自定义文本颜色、大小、字体、按钮图标等,满足各种设计需求。
  2. 丰富的视觉效果:支持实线和虚线边框、涟漪效果、动画效果,提升用户体验。
  3. 兼容性强:支持从 API 16 开始的 Android 版本,确保广泛的设备兼容性。
  4. 易于集成:通过 Gradle 轻松集成到项目中,使用简单方便。
  5. 开源社区支持:基于开源社区的强大支持,不断更新和优化,确保项目的持续发展。

安装与使用

安装

通过 Gradle 将 SegmentedButton 集成到你的项目中:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    implementation 'com.github.addisonelliott:SegmentedButton:$LATEST_VERSION'
}

注意:将 $LATEST_VERSION 替换为最新的版本号,可在 JitPack 上找到。

使用

在布局文件中使用 SegmentedButtonGroupSegmentedButton

<com.addisonelliott.segmentedbutton.SegmentedButtonGroup
    android:id="@+id/buttonGroup_lordOfTheRings"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="4dp"
    android:elevation="2dp"
    android:background="@color/white"
    app:borderColor="@color/orange_700"
    app:borderWidth="1dp"
    app:divider="@color/orange_700"
    app:dividerPadding="10dp"
    app:dividerWidth="1dp"
    app:position="0"
    app:radius="30dp"
    app:ripple="true"
    app:rippleColor="@color/green_800"
    app:selectedBackground="@color/green_900">

    <com.addisonelliott.segmentedbutton.SegmentedButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:fontFamily="@font/aniron"
        android:padding="10dp"
        app:drawable="@drawable/ic_aragorn"
        app:drawableGravity="top"
        app:selectedTextColor="@color/orange_700"
        app:text="Aragorn"
        app:textColor="@color/black" />

    <com.addisonelliott.segmentedbutton.SegmentedButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:fontFamily="@font/aniron"
        android:padding="10dp"
        app:drawable="@drawable/ic_gimli"
        app:drawableGravity="top"
        app:selectedTextColor="@color/grey_600"
        app:text="Gimli"
        app:textColor="@color/black" />

    <com.addisonelliott.segmentedbutton.SegmentedButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:fontFamily="@font/aniron"
        android:padding="10dp"
        app:drawable="@drawable/ic_legolas"
        app:drawableGravity="top"
        app:selectedTextColor="@color/yellow_200"
        app:text="Legolas"
        app:textColor="@color/black" />
</com.addisonelliott.segmentedbutton.SegmentedButtonGroup>

在 Java 代码中监听按钮状态变化:

segmentedButtonGroup.setOnPositionChangedListener(new OnPositionChangedListener() {
    @Override
    public void onPositionChanged(final int position) {
        // 处理按钮状态变化
    }
});

// 获取当前选中的按钮位置
segmentedButtonGroup.getPosition();

结语

SegmentedButton 是一个功能强大且易于使用的 Android 分段按钮控件,适用于各种需要用户选择的场景。通过丰富的自定义选项和强大的兼容性,SegmentedButton 能够帮助开发者快速实现炫酷的用户界面效果。快来尝试一下吧!

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值