美文网首页
第二章(创建自定义控件)

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

作者: Yolyn | 来源:发表于2018-05-21 09:52 被阅读16次

创建自定义控件

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


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>

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

相关文章

  • Android四种布局

    LinearLayout RelativeLayout FrameLayout TableLayout 创建自定义控件

  • 自定义控件之导航栏的实现

    我们先看一下实现效果: 使用xml自定义控件 使用代码自定义控件 首先我们创建一个工程 创建完毕之后再创建一个类来...

  • QT5自定义控件及使用控件的方法--Apple的学习笔记

    看到别人做的控件都很漂亮,自己也需要学习下自定义控件如何制作及使用。 1. 过程中问题如下 如何创建自定义控件。其...

  • 组合自定义控件的步骤详解

    Android 步骤: 1 自定义组合控件的布局settint_view.xml 2 创建一个自定义子和控件的类S...

  • Android自定义控件

    自定义控件 引入布局 创建title.xmlandroid:background="@drawable/back_...

  • iOS SDK创建

    转载自ios-创建自己的frameWork 企业开发多款app时,会有自己封装的自定义控件,而这些自定义控件可能在...

  • iOS使用Xib自定义控件

    使用xib自定义一个简单的控件 XMGShopView xib效果图: 运行时效果图: xib自定义控件的创建方法...

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

    创建自定义控件 看一下控件和布局的继承结构: 引入布局 先新建一个布局title.xml 左右两边一边一个Butt...

  • ALV的控件使用

    1简单控件创建ALV报表: 实现: 自定义输字段ALV控件实例: 以下输出结果: 在屏幕上建立ALV控件,使用SC...

  • 自定义Toolbar

    继承Toolbar 创建布局文件Toolbar.xml 创建自定义属性 使用,在主布局里添加控件

网友评论

      本文标题:第二章(创建自定义控件)

      本文链接:https://www.haomeiwen.com/subject/gixfjftx.html