0

想自定义view实现一个RadioButton的效果,"帖子"上面增加一个文字该怎么写呢?这是我写的部分内容
需要的效果:
图片描述

自定义view代码:

public class MyButton extends android.support.v7.widget.AppCompatRadioButton {

    private Paint mPaint;

    public MyButton(Context context) {
       this(context,null);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        Bitmap a=null;
//        setBackground(new BitmapDrawable(a));
        setButtonDrawable(new BitmapDrawable(a));
        mPaint=new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(0,0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText("数字",0,0,mPaint);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.demo21.MainActivity">

    <com.demo21.MyButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="帖子" />

</android.support.constraint.ConstraintLayout>
2018-07-11 提问
0 个回答

撰写答案

你可能感兴趣的

推广链接