自定义View

本文介绍两种自定义Android View的方法:一种是通过继承View并自定义属性实现文本显示;另一种是继承LinearLayout并使用自定义布局实现点击事件。文章提供了详细的代码示例及XML布局配置。

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

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
第一种方法:继承View实现自定义View
public class MyView extends View {
    private String text;
    private int textColor;
    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);
        textColor=typedArray.getColor(R.styleable.MyView_textcolor,0xffff);
        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(textColor);
        paint.setTextSize(textSize);
        canvas.drawText(text,10,100,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>

第二种方法:继承LinearLayout实现自定义View
public class MyInflatView extends LinearLayout implements View.OnClickListener {
    private TextView title;
    private ImageView icon;
    public MyInflatView(Context context) {
        super(context);
    }
    public MyInflatView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    private void initView(Context context){
    inflate(context,R.layout.custom_view,this);
        title = (TextView) findViewById(R.id.title);
        icon = (ImageView) findViewById(R.id.icon);
        icon.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v.equals(icon)){
            Toast.makeText(getContext(),"点击了",Toast.LENGTH_SHORT).show();  
        }
    }
}
XML: activity
<RelativeLayout 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"
    tools:context="com.example.fanyishuo.customitem.MainActivity">
   <com.example.fanyishuo.customitem.View.MyView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:text="小仙女"
       app:textcolor="#fff0"
       app:textsize="25sp"
       android:layout_centerInParent="true" ></com.example.fanyishuo.customitem.View.MyView>
<com.example.fanyishuo.customitem.View.MyInflatView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"></com.example.fanyishuo.customitem.View.MyInflatView>
</RelativeLayout>
custom_view:
<?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">
    <TextView
        android:id="@+id/title"
        android:text="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/icon"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值