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

简介

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

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

扩展

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

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

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

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

在上图中,文字是要居中的,而小箭头则在它旁边,并且小箭头与文字有一定的间距。

使用

接下来,只要在布局文件中用这个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是图片与文字的间距。  

原文地址:https://www.cnblogs.com/ganchuanpu/p/7596312.html