第二章(创建自定义控件)

创建自定义控件

看一下控件和布局的继承结构:


image

引入布局

  1. 先新建一个布局title.xml
<?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="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/title_back"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="@drawable/back"
        android:backgroundTint="@android:color/darker_gray" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Title Text"
        android:textSize="24sp" />

    <Button
        android:id="@+id/title_edit"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="@drawable/edit"
        android:backgroundTint="@android:color/darker_gray"
        android:gravity="center" />
</LinearLayout>

左右两边一边一个Button并设置了背景图,中间是一个TextView为标题栏

  1. 在Activity中将系统自带的标题栏隐藏掉:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
    }
}
  1. 在activity_main.xml中将title布局引入进去:
<?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.example.uicustomviews.MainActivity">

    <include layout="@layout/title"/>

</LinearLayout>

这样就将布局引入进去了,使用这种方法不管多少布局需要添加标题栏只需一行include语句就行了,这确实解决了重复编写布局代码的问题,但是如果布局中有一些控件要能够响应事件,我们还是需要在每个活动为这些控件单独编写一次事件注册的代码,这样无疑增加了很多重复的代码,所以这种情况最好的方式是用自定义控件来解决

自定义控件

  1. 新建TitleLayout继承LinearLayout

public class TitleLayout extends LinearLayout{
 //LinearLayout继承ViewGroup
    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater=LayoutInflater.from(context);
        inflater.inflate(R.layout.title,this);
        Button back=(Button)findViewById(R.id.title_back);
        Button edit=(Button)findViewById(R.id.title_edit);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }//getContext返回的是这个view所在的Context
        });
        edit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),"你点击了edit",Toast.LENGTH_SHORT).show();
            }
        });
    }


}

这里重写了LinearLayout的带两个构造参数的构造函数,在布局中引入TitleLayout控件就会调用这个构造函数,然后在构造函数中需要对标题栏进行动态加载,这就要LayoutInflater来实现了。
首先通过LayoutInflater.from(context)从上下文获取一个LayoutInflater对象,然后调用该对象的inflate方法传入两个参数,第一个参数是要加载的布局id,所以传入R.layout.title,第二个参数是要给加载好的布局再添加一个父布局,这里就指定TitleLayout。为什么要指定一个父布局?
这里涉及到一个问题:
我们在开发过程中给控件锁指定的layout_width和layout_height属性表示一个控件在容器中的大小,就是说这个控件必须在容器中这些指定的属性才有意义,否则这些属性就会失效,这就意味着如果我们直接将title加载进来而不给他指定一个父布局,那么inflate的布局的根节点(LinearLayout)的layout_width和layout_height就会失效,所以想让title的跟结点的宽高属性有效,所以就必须给他指定一个父布局

  1. 添加title布局中Button的点击事件
  2. 将自定义控件引入进去:
<?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.example.uicustomviews.MainActivity">

    <com.example.uicustomviews.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

注意的是在添加自定义控件的时候我们需要指明控件的完整类名,这里包名是不可以省略的

原创文章 43 获赞 6 访问量 797

猜你喜欢

转载自blog.csdn.net/qq_34088913/article/details/105946753