Skip to content

Repository files navigation

Android-Button2

Button2 是一个支持常用配置的 Android 按钮风格组件,支持在按钮的上下左右四个位置配置图标,并提供丰富的自定义选项。

引用方式

Gradle (Groovy)

dependencies {
    implementation 'io.github.xesam:android-button2:0.0.1'
}

Gradle (Kotlin DSL)

dependencies {
    implementation("io.github.xesam:android-button2:0.0.1")
}

基本使用

在布局文件中使用

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button Text"
    android:textColor="@android:color/black"
    android:textSize="14sp"
    app:icon_left="@drawable/ic_favorite"
    app:button_background_color="@android:color/transparent"
    app:button_border_color="#FF3700B3"
    app:button_border_width="1dp"
    app:button_corner_radius="4dp" />

在代码中使用

Button2 button2 = findViewById(R.id.button2);
button2.setText("Button Text");
button2.setIconLeft(R.drawable.ic_favorite);
button2.setButtonBackgroundColor(Color.TRANSPARENT);
button2.setButtonBorderColor(Color.parseColor("#FF3700B3"));
button2.setButtonBorderWidth(1);
button2.setButtonCornerRadius(4);

./example.jpg

配置选项

文本属性

属性名 类型 描述 默认值
android:text string 按钮文本 ""
android:textColor color 文本颜色 Color.BLACK
android:textSize dimension 文本大小 14sp

图标属性

属性名 类型 描述 默认值
icon_top reference 顶部图标 null
icon_bottom reference 底部图标 null
icon_left reference 左侧图标 null
icon_right reference 右侧图标 null
icon_width dimension 图标宽度 24dp
icon_height dimension 图标高度 24dp

图标与文本间距

属性名 类型 描述 默认值
icon_text_spacing dimension 图标与文本间距 4dp
icon_top_spacing dimension 顶部图标与文本间距 icon_text_spacing
icon_bottom_spacing dimension 底部图标与文本间距 icon_text_spacing
icon_left_spacing dimension 左侧图标与文本间距 icon_text_spacing
icon_right_spacing dimension 右侧图标与文本间距 icon_text_spacing

按钮外观

属性名 类型 描述 默认值
button_background_color color 按钮背景色 Color.TRANSPARENT
button_border_color color 按钮边框颜色 #FF3700B3
button_border_width dimension 按钮边框宽度 0dp
button_corner_radius dimension 按钮圆角半径 4dp
enable_horizontal_semicircle boolean 启用水平半圆角 false

图标可见性

属性名 类型 描述 默认值
icon_top_visible boolean 顶部图标可见性 true
icon_bottom_visible boolean 底部图标可见性 true
icon_left_visible boolean 左侧图标可见性 true
icon_right_visible boolean 右侧图标可见性 true

触摸反馈

属性名 类型 描述 默认值
enable_touch_feedback boolean 启用触摸反馈 true
pressed_background_color color 按下时背景色 Color.TRANSPARENT
pressed_alpha float 按下时透明度 0.7f
pressed_scale float 按下时缩放比例 0.95f
pressed_duration integer 按下动画时长(毫秒) 150
pressed_animation_type enum 按下动画类型 alpha

阴影属性

属性名 类型 描述 默认值
enable_shadow boolean 启用阴影 false
shadow_elevation dimension 阴影高度 0dp

动画类型

类型 描述
none 0 无动画
color 1 颜色变化动画
alpha 2 透明度变化动画
scale 3 缩放动画
all 4 所有动画效果

实用方法

设置纯文本按钮

button2.setOnlyText("Only Text");

设置纯图标按钮

button2.setOnlyIcon(R.drawable.ic_favorite);

清除所有图标

button2.clearAllIcons();

示例

图标在左侧的按钮

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Left Icon Button"
    app:icon_left="@drawable/ic_favorite" />

图标在右侧的按钮

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Right Icon Button"
    app:icon_right="@drawable/ic_favorite" />

图标在顶部的按钮

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Top Icon Button"
    app:icon_top="@drawable/ic_favorite" />

图标在底部的按钮

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bottom Icon Button"
    app:icon_bottom="@drawable/ic_favorite" />

带边框的按钮

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Border Button"
    app:button_border_color="#FF3700B3"
    app:button_border_width="2dp"
    app:button_corner_radius="8dp" />

带圆角的按钮

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Round Button"
    app:button_background_color="#FF3700B3"
    app:button_corner_radius="20dp" />

带触摸反馈的按钮

<io.github.xesam.android.button2.Button2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Touch Feedback Button"
    app:enable_touch_feedback="true"
    app:pressed_background_color="#FFBB86FC"
    app:pressed_alpha="0.8"
    app:pressed_scale="0.98"
    app:pressed_animation_type="all" />

About

一个支持常用配置的 Android 按钮风格组

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages