android图文居中按钮控件
来源:互联网 发布:淘宝用花呗付款好吗 编辑:程序博客网 时间:2024/06/05 01:51
import com.winfo.gdmsaec.app.R;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.widget.Button;public class MyIconButton extends Button { protected int drawableWidth; protected DrawablePositions drawablePosition; protected int iconPadding; Rect bounds; private enum DrawablePositions { NONE, LEFT_AND_RIGHT, LEFT, RIGHT } public MyIconButton(Context context) { super(context); bounds = new Rect(); } public MyIconButton(Context context, AttributeSet attrs) { super(context, attrs); bounds = new Rect(); applyAttributes(attrs); } protected void applyAttributes(AttributeSet attrs) { if (null == bounds) { bounds = new Rect(); } TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyIconButton); int paddingId = typedArray.getDimensionPixelSize(R.styleable.MyIconButton_iconPadding, 0); setIconPadding(paddingId); typedArray.recycle(); } public void setIconPadding(int padding) { iconPadding = padding; requestLayout(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); Paint textPaint = getPaint(); String text = getText().toString(); textPaint.getTextBounds(text, 0, text.length(), bounds); int textWidth = bounds.width(); int factor = (drawablePosition == DrawablePositions.LEFT_AND_RIGHT) ? 2 : 1; int contentWidth = drawableWidth + iconPadding * factor + textWidth; int horizontalPadding = (int) ((getWidth() / 2.0) - (contentWidth / 2.0)); setCompoundDrawablePadding(-horizontalPadding + iconPadding); switch (drawablePosition) { case LEFT: setPadding(horizontalPadding, getPaddingTop(), 0, getPaddingBottom()); break; case RIGHT: setPadding(0, getPaddingTop(), horizontalPadding, getPaddingBottom()); break; case LEFT_AND_RIGHT: setPadding(horizontalPadding, getPaddingTop(), horizontalPadding, getPaddingBottom()); break; default: setPadding(0, getPaddingTop(), 0, getPaddingBottom()); } } @Override public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); if (left != null && right != null) { drawableWidth = left.getIntrinsicWidth() + right.getIntrinsicWidth(); drawablePosition = DrawablePositions.LEFT_AND_RIGHT; } else if (left != null) { drawableWidth = left.getIntrinsicWidth(); drawablePosition = DrawablePositions.LEFT; } else if (right != null) { drawableWidth = right.getIntrinsicWidth(); drawablePosition = DrawablePositions.RIGHT; } else { drawablePosition = DrawablePositions.NONE; } requestLayout(); }}
使用时在xml文件头部加上
xmlns:padding="http://schemas.android.com/apk/res/项目包名"
<com....widget.MyIconButton
android:id="@+id/main_system_different"
style="@style/MyIconButton"
android:background="@drawable/main_btn_right_bg"
android:drawableLeft="@drawable/system_set"
android:text="@string/system_setting"
padding:iconPadding="3dp" />这个值是图片和文字之间的距离 使用者可根据情况设定
<style name="MyIconButton">
<item name="android:textSize">16sp</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">45dp</item>
<item name="android:layout_weight">1</item>
<item name="android:textColor">@color/black</item>
</style>
- android图文居中按钮控件
- 自定义图文居中控件
- IOS图文按钮居中排列
- Android开源项目——设置图文居中的按钮 IconButton
- Android开源项目——设置图文居中的按钮 IconButton
- Android控件居中显示
- Android ImageSpan的图文居中对齐
- Android TextView内容居中和控件居中
- 让android控件水平居中
- 让android控件水平居中
- Android布局 使控件居中
- Android控件--按钮
- android 控件 按钮(Button)
- Android自定义控件按钮
- android 自定义按钮控件
- android控件内容居中及控件布局居中
- android 一行两个按钮居中 布局
- Android自定义导航图文按钮ImageTextButton
- iOS 图片轮播器
- EventBus的使用与深入学习
- python链接mysql数据库并以键值对的形式查询显示
- cmake入门教程
- 很牛的netcat
- android图文居中按钮控件
- HTML地址栏(hash)获取并转码(个人菜鸟笔记)
- php pdo链接mysql数据库
- 文本框组件实现填空效果,自动跟随中文变大.
- C++中对象或其对象指针的赋值
- 常用的正则表达式---学习笔记(一)
- ajax,php,jsp,ajax跨域调用
- 互联网开发全面进入开发直接运维的时代
- 修改searchBar两种后背景颜色