自定义View

一、预备知识

1. View和ViewGroup的关系

ViewGroup继承View,所有控件和布局都直接或间接的继承View,所有布局都继承ViewGroup。

2. Android坐标系和View坐标系

3. 在布局中引入布局

使用方式:

前提:已编写好 title.xml 和 main.xml 布局文件,在main.xml 布局文件中引入 title.xml 布局文件。

引入方式:通过 include 标签引入布局文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
    
    <include layout="@layout/title"/> <!--引入title.xml布局文件-->
    
</LinearLayout>

二、自定义View的基本流程

1. 编写布局文件

编写自定义View的布局文件,设置相应的样式。

举例:

自定义View的布局文件:标题栏

<!--自定义View的布局文件:标题栏-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
    android:background="@drawable/title_bg">
    
    <Button
    	android:id="@+id/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/back_bg"
        android:text="返回"
        android:textColor="#fff"/>
    
    <TextView
    	android:id="@+id/title_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="标题"
        android:textColor="#fff"
        android:textSize="24sp"/>
    
    <Button
    	android:id="@+id/title_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/edit_bg"
        android:text="编辑"
        android:textColor="#fff"/>
    
</LinearLayout>

2. 创建自定义控件类

根据自定义类的需求,继承相应的View或者ViewGroup。编写带有两个参数的构造函数,在其函数体内完成相应的事件绑定等。

举例:

创建标题栏控件:

public class TitleLayout extends LinearLayout{
    //
    public TitleLayout(Context context, AttributeSet attrs){
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);
        Button titleBack = (Button) findViewById(R.id.title_back);
        Button titleEdit = (Button) findViewById(R.id.title_edit);
        titleBack.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                ((Activity) getContext()).finish(); 
            }
        });
        titleEdit.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(getContext(), "点击了编辑按钮", Toast.LENGTH_SHORT).show();
            }
        })
    }
}

3. 使用自定义控件

类似使用Android控件一样使用,但使用名称为类的全限定路径名。

举例:

在MainActivity中使用自定义的 TitleLayout 。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
    
    <com.joygin.www.TitleLayout
    	android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>
posted on 2020-12-25 02:27  零_壹  阅读(105)  评论(0)    收藏  举报