自定义TextView

第一种方法,只是单纯的实现一个自定义TextView:
public class MyView extends View {

    private String text;
    private int color;
    private float textsize;

 public MyView(Context context) {
        super(context);
        init(null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs){
        super(context,attrs);
        init(attrs);
    }
    private void init(@Nullable AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyView);
        text = typedArray.getString(R.styleable.MyView_text);
        color = typedArray.getColor(R.styleable.MyView_textcolor, 0xffffff);
        textsize = typedArray.getDimension(R.styleable.MyView_textsize, 16);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setTextSize(textsize);
        canvas.drawText(text,300,300,paint);
    }
}
在values中创建一个attrs
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="text" format="string"></attr>
        <attr name="textsize" format="dimension"></attr>
        <attr name="textcolor" format="color"></attr>
    </declare-styleable>

</resources>
 布局文件:
<View.MyView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    app:text="申文静"
    app:textcolor="#000000"
    app:textsize="16sp"
    />
第二种方法继承一个linerlayout,inflate一个布局进去
public class MyTextView extends LinearLayout {

    private TextView biaoti;
    private ImageView img;

    public MyTextView(Context context) {
        super(context);
    }
    public MyTextView(Context context, @Nullable AttributeSet attrs){
        super(context,attrs);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.second,this);
        biaoti = (TextView) findViewById(R.id.biaoti);
        img = (ImageView) findViewById(R.id.img);
        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),"我在自定义view中被点击了",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
布局文件;
<View.MyTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">
</View.MyTextView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/biaoti"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="申文静"
        android:textSize="20sp"
        />
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值