自定义View两种使用方法

本文介绍如何在Android中创建自定义View及引用布局的方式。通过继承View类并重写onDraw方法实现自定义View;同时展示如何通过XML声明自定义属性,并在Java代码中获取这些属性值。此外,还介绍了另一种自定义View的方法——引用现有的布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一种是自定义属性的View,另外一种是引用定义好的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ff.customview.MainActivity">

    <com.ff.customview.MyTextView
        android:id="@+id/mytextview"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:Text="这是使用attrs定义的属性"
        app:TextColor="#f00"
        app:TextSize="30sp"/>

    <com.ff.customview.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </com.ff.customview.MyCustomView>

</LinearLayout>

自定义TextView继承View
public class MyTextView extends View {
    private int TextColor;
    private String Text;
    private float TextSize;

    public MyTextView(Context context) {
        super(context);
        initView(null);

    }

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

    private void initView(@Nullable AttributeSet attrs) {
        //得到咱们自定义的attrs中的数据
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
        Text = typedArray.getString(R.styleable.MyTextView_Text);
        TextSize = typedArray.getDimension(R.styleable.MyTextView_TextSize, 15);
        TextColor = typedArray.getColor(R.styleable.MyTextView_TextColor, Color.YELLOW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置画笔的颜色,和大小
        Paint paint = new Paint();
        paint.setColor(TextColor);
        paint.setTextSize(TextSize);
        paint.setAntiAlias(true);
        /**
         * 画出来一个view
         * 由于画的是一个textview,第一个参数是就是text内容
         * 第一个参数距离 x坐标
         * 第二个参数是 距离 y坐标
         * 第三个参数是画笔
         */
        canvas.drawText(Text, 20, 50, paint);
    }
}

引用布局自定义view,继承LinearLayout 
public class MyCustomView extends LinearLayout {
    public MyCustomView(Context context) {
        super(context);
    }

    public MyCustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(final Context context) {
        inflate(context, R.layout.layout, this);
        ImageView img = (ImageView) findViewById(R.id.layout_image);
        TextView text = (TextView) img.findViewById(R.id.item_textview);
        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "我被点击了", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
//在values文件夹下新建立一个attrs文件,里面存放你要自定义的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTextView">
        <attr name="Text" format="string"></attr>
        <attr name="TextSize" format="dimension"></attr>
        <attr name="TextColor" format="color"></attr>
    </declare-styleable>

</resources>
//下面是第二种方法中使用的布局,这里只是简单的布局,根据自己需求来定义
<?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"
              >

    <ImageView
        android:id="@+id/layout_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>

    <TextView
        android:id="@+id/item_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是item"/>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值