自定义textview背景图

来源:互联网 发布:sqlserver安装错误 编辑:程序博客网 时间:2024/05/16 09:35

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:IWindTopBgView="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:orientation="horizontal">


            <wind.ui.IWindTopBgView
                android:id="@+id/btn_chat"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_marginRight="1dip"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="4dip"
                android:text="聊天"
                android:textColor="@color/color_white"
                android:textSize="13dip"
                IWindTopBgView:borderRectColor="@color/iwindtab_red" />


            <wind.ui.IWindTopBgView
                android:id="@+id/btn_contact"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_marginLeft="1dip"
                android:layout_weight="1"
                android:gravity="center"
                android:padding="4dip"
                android:text="通讯录"
                android:textColor="@color/color_white"
                android:textSize="13dip"
                IWindTopBgView:borderRectColor="@color/iwindtab_orange" />


        </LinearLayout>


        <ImageView
            android:id="@+id/iv_unread"
            android:layout_width="10dip"
            android:layout_height="10dip"
            android:layout_gravity="center_horizontal"
            android:layout_marginRight="20dip"
            android:layout_marginTop="3dip"
            android:src="@drawable/iwind_update_notice"
            android:visibility="invisible" />
    </FrameLayout>
</LinearLayout>


values文件夹下新建attr文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="searchEditTextStyleable">        
        <attr name="btnBackground" format="reference" />
        <attr name="btnText" format="reference|string" />
        <attr name="btnTextcolor" format="reference|color" />
        <attr name="btnTextsize" format="dimension"/>   
        <attr name="btnVisibility" format="string"/>     
    </declare-styleable>
    <declare-styleable name="IWindTopBgView"   >
            <attr name="borderRectColor" format="reference|color"/>
    </declare-styleable>
</resources>

自定义控件

public class IWindTopBgView extends TextView {
    private int mBorderColor = Color.TRANSPARENT;
    private Paint mPaint;
    public IWindTopBgView(Context context) {
        this(context,Color.TRANSPARENT);
    }
    public IWindTopBgView(Context context, int color) {
        super(context);
        mBorderColor = color;
        init(context);
    }


    public IWindTopBgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.IWindTopBgView, 0, 0);
        mBorderColor = attributes.getColor(R.styleable.IWindTopBgView_borderRectColor, Color.TRANSPARENT);
        init(context);
    }


    public void setBorderColor(int color){
        mPaint.setColor(mBorderColor);
    }


    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(mBorderColor);
        mPaint.setStyle(Paint.Style.STROKE);
    }


    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        //绘制矩形框
        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
    }
}

0 0