Android Studio 创建自定义控件的方法

 更新时间:2020年06月12日 14:49:42   作者:null;  
这篇文章主要介绍了Android Studio 创建自定义控件的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

我们知道,当系统控件并不能满足我们的需求时,我们就需要来创建自定义控件,主要有两种方法

(1)引入布局

下面来自定义一个控件,iPhone的标题栏,创建一个标题栏并不是什么难事,加入两个button一个TextView就行了,可是在我们的应用中,有很多页面都是需要这样的标题栏,我们不可能每个活动都写一遍布局,这个时候我们就可以用引用布局的方法,新建一个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="#817D7D"
  >

  <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:text="back"
    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:textColor="#c0c0c0"
    android:textSize="24sp"
    android:text="title text" />

  <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:textColor="#fff"
    android:text="edit" />
</LinearLayout>

现在标题栏已经写好了,接下来就要在程序中使用,修改activity_main.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="match_parent"
 >

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

</LinearLayout>

我们只要通过一句include语句引进来就行了

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

最后我们需要在MainActivity中将系统自带的标题栏屏蔽

package com.example.ch03;

import android.drm.DrmStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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

最后来看一下效果

(2)注册点击事件

在上面我们看到,每个界面的返回按钮功能都是一样的,即销毁当前活动,我们不可能在每个活动中都重新注册,所以使用自定义控件的方式来解决
新建TitleLayout,成为标题栏控件

public class TitleLayout extends LinearLayout {
			public TitleLayout(Context context, AttributeSet attrs){
 				 super(context,attrs);
  			 LayoutInflater.from(context).inflate(R.layout.title,this);

我们重写了LinearLayout中带参数的构造函数,引入TitleLayout控件就会调用这个构造函数,然后对标题栏进行动态加载,就需要借助LayoutInflater实现。通过LayoutInflater的from方法构建一个LayoutInflater对象,调用inflate()方法动态加载一个布局文件

然后在布局文件中添加自定义控件,修改activity_main.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="match_parent"
 >
<com.example.ch03.TitleLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

</LinearLayout>

重新运行一下,效果是一样的

下面来给按钮注册点击事件,修改TitleLayout中的代码

package com.example.ch03;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs){
  super(context,attrs);
  LayoutInflater.from(context).inflate(R.layout.title,this);

  Button titleBack = findViewById(R.id.title_back);
  Button titleEdit = 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(),"You click edit button",
          Toast.LENGTH_LONG).show();
    }
  });
}

}

重新运行一下,然后点击edit按钮

到此这篇关于Android Studio 创建自定义控件的方法的文章就介绍到这了,更多相关Android Studio自定义控件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android实现长图文截图功能实例代码

    Android实现长图文截图功能实例代码

    这篇文章主要给大家介绍了关于Android实现长图文截图功能的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • viewpager+photoview实现图片查看器

    viewpager+photoview实现图片查看器

    这篇文章主要为大家详细介绍了viewpager+photoview实现图片查看器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 详解Android的四大应用程序组件

    详解Android的四大应用程序组件

    这篇文章主要介绍了Android的应用程序组件的相关资料,帮助大家更好的理解和使用Android,感兴趣的朋友可以了解下
    2021-01-01
  • Kotlin 协程思维模型的引入使用建立

    Kotlin 协程思维模型的引入使用建立

    这篇文章主要为大家介绍了Kotlin 协程思维模型的引入使用及建立详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • 实例分析Android中HandlerThread线程用法

    实例分析Android中HandlerThread线程用法

    本篇文章主要给大家介绍了Android HandlerThread使用介绍以及源码解析,有需要的朋友参考学习下吧。
    2017-12-12
  • Android开发实现浏览器全屏显示功能

    Android开发实现浏览器全屏显示功能

    这篇文章主要介绍了Android开发实现浏览器全屏显示功能,涉及Android布局修改及相关属性动态设置操作技巧,需要的朋友可以参考下
    2017-09-09
  • Android 通过Messager与Service实现进程间双向通信案例详解

    Android 通过Messager与Service实现进程间双向通信案例详解

    这篇文章主要介绍了Android 通过Messager与Service实现进程间双向通信案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • Android开发之绘制平面上的多边形功能分析

    Android开发之绘制平面上的多边形功能分析

    这篇文章主要介绍了Android开发之绘制平面上的多边形功能,结合实例形式分析了Android多边形图形绘制的原理、步骤、相关操作技巧与注意事项,需要的朋友可以参考下
    2017-09-09
  • 在Android中创建widge组件的步骤

    在Android中创建widge组件的步骤

    Android Widget 是一种轻量级的小部件,可以直接在主屏幕上显示实时数据,提供简单交互,它们主要用于展示简单信息或快捷功能,帮助用户更快、更方便地与应用交互,接下来通过本文给大家介绍创建 Android Widget 的步骤,感兴趣的朋友跟随小编一起看看吧
    2024-08-08
  • 谈谈对Android View事件分发机制的理解

    谈谈对Android View事件分发机制的理解

    本篇文章主要介绍了谈谈对Android View事件分发机制的理解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01

最新评论