Android开源项目——设置图文居中的按钮 IconButton

时间:2020-12-23 04:54:05

本文介绍一下一个小众的开源项目——IconButton。

本文原创,转载请注明出处:

http://blog.csdn.net/maosidiaoxian/article/details/43560209

简介

这是一个老外两年前写的项目,见:https://github.com/pnc/IconButton。它可以使一个Button的图片与文字一起居中,如下图:

Android开源项目——设置图文居中的按钮 IconButton

以前要实现这种效果却是用一个Layout和一个ImageView一个TextView来实现它的同学,看到这个项目的这一刻,满满的感动有木有?

扩展

为方便在项目中使用,及实现项目里的其它需求,我fork了此项目,对它进行了修改和扩展,并发布到jCenter中。另外,表示老外真的很友好。

为实现设计师设计的界面,我对它的扩展如下:

1.扩展了居中策略。原来的IconButton中,是图片、图片与文字的间距和文字一起居中的。我扩展为默认是这三个一起居中,但也可以设置为仅文字居中或者文字与间距居中。

2.增加了IconCheckBox。这主要是为实现下图中的效果:

Android开源项目——设置图文居中的按钮 IconButton

在上图中,文字是要居中的,而小箭头则在它旁边,并且小箭头与文字有一定的间距。
我的项目地址为:https://github.com/msdx/IconButton。扩展的内容见develop分支。

使用

对于使用Gradle 构建的Android Studio项目,使用方式很简单,先确定你在根项目的build.gradle中声明了JCenter*仓:
repositories {
    jcenter()
}

然后在模块中的build.gradle中添加此依赖:

dependencies {
    compile 'com.githang:com-phillipcalvin-iconbutton:2.0.2'
}

对于Eclipse项目,且不是用Gradle构建的,那就乖乖地拷贝代码或导入项目吧。


接下来,只要在布局文件中用这个IconButton或IconCheckBox就可以了,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

<!-- ... -->

  <com.phillipcalvin.iconbutton.IconButton
      android:id="@+id/search"
      android:drawableLeft="@drawable/action_search"
      android:text="@string/search"
      app:iconPadding="10dp" />

其中的iconPadding是图片与文字的间距。

其它扩展使用见项目中的app模块的代码。