Android 知识点的小总结Android开发经验谈Android开发

第九章 自定义控件

2017-09-12  本文已影响70人  忆念成风

1.为什么要用到自定义控件

  在介绍自定义控件之前,说说控件的基本结构,如下图所示,所有的控件都直接或者间接的继承View的,所有的布局都是继承ViewGroup的。我们都知道继承的原理,是在继承父属性的基础上还拥有自己的特别属性,因此我们使用的控件其实也就是在View的基础之上又添加自己的功能。所以ViewGroup是一个特殊的View,可以包含很多子View和子ViewGroup,用于放置控件和布局的容器。
  但是,我们的需求如果仅靠系统系统的控件,无法满足的时候呢?这个时候怎么办?继承就给了一个很好解决办法来创建自定义的控件。


控件的基本结构图

2.引入布局

  在实际的开发过程中,我们的UI小姐姐在给你设计开发图的时候,不会不给你的页面加上顶部标题,因为你需要返回的,你需要在你的顶部标题栏中进行各种操作的啊。这些标题栏的内容一般都是差不多的,如果你不想重复代码的话,只需要将这个顶部标题栏自己单独写在一个xml文件里面,在你需要的加上标题的时候,通过 include来加上引入你写的xml文件就可以了,这样减少了代码,减轻了工作,提高了代码的复用率,是不是美滋滋。
1.新建一个head_title.xml的layout文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#0093fe">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:src="@mipmap/nav_back_white"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:text="标题"
        android:layout_centerHorizontal="true"
        android:textColor="#fff"
        android:gravity="center"
        android:textSize="18sp"/>
    
    <ImageView
        android:layout_width="50dp"
        android:layout_height="match_parent" 
        android:src="@mipmap/nav_share"
        android:layout_alignParentRight="true"
        android:scaleType="centerInside"/>

</RelativeLayout>
head_title

2.隐藏系统自带的标题
方法一:代码中修改

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

方法二:修改主题

未修改之前的
 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
修改 parent属性
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

3.通过include 引用head_title.xml 文件。 格式:<include layout="@layout/被引用的文件名称"/>

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.demo.demo4.MainActivity">
    
    <include layout="@layout/head_title"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

引用之后

使用这种方式,不管多少布局都能通过include 套用进去。

3.创建自定义控件

  引入布局解决了代码的重复编写,但是如果在布局控件中有一些控件需要响应时间的haul,我们还得为每个活动中的响应事件编写代码。比如标题栏的返回按钮,不管在别的什么活动中,它的功能都是销毁当前的活动,同样的代码,就不要写很多遍了,你觉得是吧。

3.1 自定义控件的编写

1.自定义一个TitleLayout 继承LineraLayout,通过LayoutInflate 的from()的方法构建一个LayoutInflater对象,调用inflate() 方法动态加载布局文件,第二个参数是给加载好的布局再添加一个父布局。

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context) {
        this(context,null);
    }

    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TitleLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.head_title,this);
    }
}

2.在activity_main.xml文件中添加控件,添加的方式和普通控件是一个样的。我们自定义控件引用的时候需要引用全包名。

    <com.demo.demo4.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
自定义包名引用

3.为各个按钮添加点击事件和处理

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context) {
        this(context,null);
    }

    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TitleLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.head_title,this);
        initView();

    }

    private void initView() {
        ImageView backiv= (ImageView) findViewById(R.id.back_iv);
        ImageView shareiv= (ImageView) findViewById(R.id.share_iv);
        TextView title=(TextView) findViewById(R.id.title_tv);

        backiv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });
        shareiv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });
    }

自定义的道路还很长,这里讲的是最基础的,后续会陆续更新。

上一篇 下一篇

猜你喜欢

热点阅读