自定义控件让TextView、Button的drawableLeft和drawableRight与文本一起居中显示

  TextView的drawableLeft、drawableRight和drawableTop是一个常用、好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享。

布局XML

<com.assistant.expand.customview.DrawableCenterButton
                        android:gravity="left|center_vertical"
                        android:drawableLeft="@drawable/icon_erweima"
                        android:drawablePadding="5dp"
                android:id="@+id/btn_scale"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@android:color/transparent"
                android:singleLine="true"
                android:text="扫描二维码签到"
                android:textColor="@color/color_button2"
                android:textSize="17sp" />
/**
 * drawableLeft与文本一起居中显示
 *
 *
 */
public class DrawableCenterTextView extends TextView {

    public DrawableCenterTextView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public DrawableCenterTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawableCenterTextView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
            Drawable drawableLeft = drawables[0];
            if (drawableLeft != null) {
                float textWidth = getPaint().measureText(getText().toString());
                int drawablePadding = getCompoundDrawablePadding();
                int drawableWidth = 0;
                drawableWidth = drawableLeft.getIntrinsicWidth();
                float bodyWidth = textWidth + drawableWidth + drawablePadding;
                canvas.translate((getWidth() - bodyWidth) / 2, 0);
            }
        }
        super.onDraw(canvas);
    }
}

下面是用Button的Right 例子

/**
 * drawableRight与文本一起居中显示
 *
 *
 */
public class DrawableCenterButton extends Button {

	public DrawableCenterButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public DrawableCenterButton(Context context, AttributeSet attrs,
	int defStyle) {
		super(context, attrs, defStyle);
	}

	public DrawableCenterButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Drawable[] drawables = getCompoundDrawables();
		if (drawables != null) {
			Drawable drawableLeft = drawables[2];
				if (drawableLeft != null) {

				float textWidth = getPaint().measureText(getText().toString());
				int drawablePadding = getCompoundDrawablePadding();
				int drawableWidth = 0;
				drawableWidth = drawableLeft.getIntrinsicWidth();
				float bodyWidth = textWidth + drawableWidth + drawablePadding;
				setPadding(0, 0, (int)(getWidth() - bodyWidth), 0);
				canvas.translate((getWidth() - bodyWidth) / 2, 0);
			}
		}
		super.onDraw(canvas);
	}
}
				
时间: 2024-08-30 09:10:44

自定义控件让TextView、Button的drawableLeft和drawableRight与文本一起居中显示的相关文章

【Android】自定义控件让TextView的drawableLeft与文本一起居中显示

前言  TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享.   声明 欢迎转载,请注明出处! 博客园:http://www.cnblogs.com/ 农民伯伯: htt

Android中自定义控件让TextView的drawableLeft与文本一起居中

前言 TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享. 一.效果图 查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bian

Android 让自定义TextView的drawableLeft与文本一起居中_Android

前言   TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享. 正文  一.效果图  二.实现代码  自定义控件 /** * drawableLeft与文本一起居中显示

安卓在代码中设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom

安卓在代码中设置TextView的drawableLeft.drawableRight.drawableTop.drawableBottom TextView的xml文件 <TextView android:id="@+id/tv_versionstatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePa

android-Android自定义控件中为Button设置自定义监听器出现空指针。

问题描述 Android自定义控件中为Button设置自定义监听器出现空指针. Android自定义TopBar中给ImageButton设置自定义点击监听器,当点击ImageButton时在listener处出现NullPointerException. 会出错的地方我在后面写了//TODO . 请问为什么会出现listener没有被实例化的情况? 还有为什么在MainActivity中使用findViewById实例化TopBar也会出现不能实例化控件的情况? IDE : Android S

android-关于在TextView中设置DrawableLeft的问题

问题描述 关于在TextView中设置DrawableLeft的问题 xml中的textView: <TextView android:id="@+id/bookTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawableLeft=&quo

android-Android调试- 删除TextView,Button和Layout引发崩溃

问题描述 Android调试- 删除TextView,Button和Layout引发崩溃 当我点击按钮来删除一个TextView ,Button 和 LinearLayout.时,程序崩溃.下面是logcat输出,当我想在118行删除View时就崩溃了. tasklayout.removeView(taskbutton); 04-15 14:35:58.222: E/AndroidRuntime(632): java.lang.NullPointerException 04-15 14:35:5

Android自动生成了一堆的button列表用,因为屏幕高度问题没显示完全。

问题描述 Android自动生成了一堆的button列表用,因为屏幕高度问题没显示完全. 想要的效果是这样的,也就是可以上下拖动,顶部蓝色layout固定不动.代码如下: //布局如下: <!-- 顶部蓝色工具栏 --> <RelativeLayout android:id=""@+id/fujin_top_llayout"" android:layout_width=""fill_parent"" andr

Android UI设计系列之自定义TextView属性实现带下划线的文本框(4)_Android

在Android开发过程中,如果Android系统自带的属性不能满足我们日常开发的需求,那么就需要我们给系统控件添加额外的属性了.假如有个需求是实现带下划线的文本显示(下划线),如果不使用自定义属性的话实现起来也不太难(起码我认为的实现方式是有许多种的),今天就讲解一下如何使用自定义属性来实现上述带下划线的文本框吧.还好Android中自定义属性不是很复杂,也可以归纳为三步走吧. 老规矩,还是先贴出工程目录吧: 一.添加属性文件 在values文件夹中新建attrs.xml文件,在文件中新建属性