【Android 自定义控件】TextView的drawable与text一起居中

本文解决了Android中TextView的drawableleft, drawableright, drawabletop, drawablebottom与文字一起设置gravity='center'居中的问题。通过自定义DrawableCenterTextView控件,实现了图标的对齐效果。代码示例中利用‘正’字符调整文本宽度,可根据实际需求调整。" 71597601,6650138,Linux进程监控与管理详解,"['Linux', '操作系统', '进程管理', '系统监控', '命令行工具']

解决的问题:drawableleft,drawableright,drawabletop,drawablebottom与text一起gravity="center"居中的问题。


自定义一个TextView控件,叫做:DrawableCenterTextView.java


package com.dzq.smswclient.widget;

/**
 * Data:2016-05-26 16:30
 * Created by YJG
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * drawableTop与文本一起居中显示
 * com.yjg.myapplication2.widget.DrawableCenterTextView
 */
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 drawableTop = drawables[1];
            for (int i = 0; i < drawables.length; i++) {
                Drawable drawable = drawables[i];
                if (drawable != null) {
                    if (i == 0 || i==3) {
                        //drawableLeft or drawableRight with singline text four word ()
//                        float textWidth = getPaint().measureText(getText().toString());
                        float textWidth = getPaint().measureText("正正正正");
                        int drawablePadding = getCompoundDrawablePadding();
                        int drawableWidth = 0;
                        drawableWidth = drawable.getIntrinsicWidth();
                        float bodyWidth = textWidth + drawableWidth + drawablePadding;
                        canvas.translate((getWidth() - bodyWidth) / 2, 0);
                    } else if (i == 1 || i==4) {
                        //drawableTop or drawableBottom
                        float textHeight = getPaint().measureText("正");
                        int drawablePadding = getCompoundDrawablePadding();
                        int drawableHeight = drawableTop.getIntrinsicHeight();
                        float bodyWidth = textHeight + drawableHeight + drawablePadding;
                        canvas.translate(0, (getHeight() - bodyWidth) / 2);
                    }
                }
            }
        }
        super.onDraw(canvas);
    }
}

注:上面代码可以看到,用“正”来给文本宽度,这里可以根据需要调整“正”的个数


具体使用:(其中一行)

drawableLeft或者drawableRight与text



<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="@drawable/shape_divider_8_8"
            android:orientation="horizontal"
            android:showDividers="middle">
            <com.dzq.smswclient.widget.DrawableCenterTextView
                android:id="@+id/tv_office"
                style="@style/DrawablecenterTextView_InsideSys"
                android:background="@color/inside_11"
                android:drawableLeft="@drawable/btn_platform_office"
                android:text="移动\n办公"
            />
            <com.dzq.smswclient.widget.DrawableCenterTextView
                android:id="@+id/tv_investment"
                style="@style/DrawablecenterTextView_InsideSys"
                android:background="@color/inside_12"
                android:drawableLeft="@drawable/btn_platform_investment"
                android:text="招商\n引资"
            />
        </LinearLayout>

style.xml样式

<!-- 内部系统首页textview的样式 -->
    <style name="DrawablecenterTextView_InsideSys">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:drawablePadding">16dp</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">@dimen/text_size_18</item>
    </style>

再如:

drawabletop,drawablebottom与text

图示:



 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <com.dzq.smswclient.widget.DrawableCenterTextView
                android:id="@+id/tv_economy"
                style="@style/DrawablecenterTextView_Home"
                android:background="@color/orange_economy_nor"
                android:drawableTop="@drawable/btn_home_tradeinquires"
                android:text="经贸资讯"
            />
            <com.dzq.smswclient.widget.DrawableCenterTextView
                android:id="@+id/tv_investment"
                style="@style/DrawablecenterTextView_Home"
                android:background="@drawable/btn_home_investment"
                android:drawableTop="@drawable/bg_transparent"
                android:text="投资服务"
            />
        </LinearLayout>

style.xml

<style name="DrawablecenterTextView_Home">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:drawablePadding">12dp</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:singleLine">true</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">@dimen/text_size_14</item>
    </style>









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leafseelight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值