创建自定义控件

自定义控件 - 引入布局

常见控件和布局的继承结构


image

所有的控件都是直接或者间接继承于View
所有的布局都是直接或者间接继承于ViewGroup
View是Android中最基本的一种UI控件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件。
ViewGroup是特殊的一种View,是一个用于放置控件和布局的容器

以添加iPhone风格的标题栏为例:
当多个活动界面都要使用这个标题栏时,我们可以通过引入布局的方式,这样可以避免每个活动界面都要写一遍同样的标题代码,减少代码重复。

创建布局文件

新建一个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:background="@drawable/title_bg">

    <Button
        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="Back"
        android:textColor="#fff"
        android:id="@+id/title_back"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Title Text"
        android:id="@+id/title_text"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="24sp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title_edit"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/edit_bg"
        android:text="Edit"
        android:textColor="#fff"/>

</LinearLayout>

android:background用于为控件或布局制定一个背景,可以使用颜色或者图片
android:layout_margin 指定控件在上下左右方向上偏移的距离

引入布局

在创建了布局后,我们要引入布局。在需要引入界面的Activity界面布局中,添加<include layout="@layout/title"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <include layout="@layout/title"/>
</LinearLayout>

隐藏系统自带的标题栏

通过调用getSupportActionBar()方法来获得ActionBar的实例,然后再调用ActionBar的hide()方法将标题栏隐藏。

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();
        }
    }
}
image

创建自定义控件

引入布局的方式虽然可以减少很多重复的布局代码,但是当布局中的控件要求能够响应事件,我们还是需要在每个活动中去为这些控件单独编写事件响应代码。以标题栏为例,这些控件在每一个布局中所需的功能都是一样的,这时我们可以使用自定义控件的方式,避免每个活动都要去编写同样的代码。

新建TitleLayout

新建TitleLayout并继承LinearLayout。

public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context , AttributeSet attributeSet){
        super(context,attributeSet);
        LayoutInflater.from(context).inflate(R.layout.title,this);  
    }
}

重写LinearLayout中带有两个参数的构造函数。
通过LayoutInflater来对标题栏进行动态加载。
LayoutInflater.from(context).inflate(R.layout.title,this);

  • from()方法构建出一个LayoutInflater对象
  • 调用inflate()方法就可以动态加载一个布局文件。inflate接受两个参数
    • 一个是加载的布局文件id
    • 另一个是给加载好的布局再添加一个父布局

修改活动布局.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

  //完整类名
   <com.example.hwy01.uicustomviews.TitleLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></com.example.hwy01.uicustomviews.TitleLayout>

</LinearLayout>

添加自定义控件需要先指明控件的完整类名,如代码中的com.example.hwy01.uicustomviews.TitleLayout

添加按钮注册事件

修改TitleLayout

public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context , AttributeSet attributeSet){
        super(context,attributeSet);

        LayoutInflater.from(context).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 view) {
           ((Activity)getContext()).finish();
            }
        });

        edit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(),"You clicked Edit Button",Toast.LENGTH_SHORT).show();
            }
        });
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容