Android组合控件自定义标题栏

 更新时间:2018年11月20日 17:13:17   作者:Simple-Coder  
这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android简单的自定义标题栏,供大家参考,具体内容如下

android自定义控件向来都是开发者最头疼的,但是我们要有那种迎难而上的精神。

MainActivity

package com.example.customview;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

/*
 android自定义标题组合控件
 步骤:
 1.首先写出需要功能的布局xml,分析布局的父控件是谁?
 例如水平布局 父控件应该是linearlayout较为合适
 2.创建自定义控件类并继承xml父控件
 3.在构造方法中使用layoutInflat动态加载布局
 */
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();
  }
 }

}

TitleLayout.class

package com.example.customview.custom;

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.TextView;
import android.widget.Toast;

import com.example.customview.R;

/**
 * 自定义标题栏 并赋有点击事件
 */
public class TitleLayout extends LinearLayout implements View.OnClickListener {
 private Button btback, btopen;
 private TextView tvtitle;

 public TitleLayout(Context context, AttributeSet attrs) {
  super(context, attrs);
  //动态加载标题栏布局
  LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
  initView();
 }

 private void initView() {//初始化控件
  btback = (Button) findViewById(R.id.btback);
  btback.setOnClickListener(this);
  btopen = (Button) findViewById(R.id.btopen);
  btopen.setOnClickListener(this);
  tvtitle = (TextView) findViewById(R.id.tvtitle);
  tvtitle.setOnClickListener(this);
 }

 @Override
 public void onClick(View view) {//监听点击事件
  switch (view.getId()) {
   case R.id.btback:
    ((Activity) getContext()).finish();
    Toast.makeText(getContext(), "销毁当前Activity", Toast.LENGTH_SHORT).show();
    break;
   case R.id.btopen:
    Toast.makeText(getContext(), "展开", Toast.LENGTH_SHORT).show();
    break;
   case R.id.tvtitle:
    Toast.makeText(getContext(), "标题", Toast.LENGTH_SHORT).show();
    break;
  }
 }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.customview.MainActivity">

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

 <com.example.customview.custom.TitleLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />
</LinearLayout>

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.customview.MainActivity">

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

 <com.example.customview.custom.TitleLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />
</LinearLayout>

粘贴以上代码就可以运行了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android编程获取系统隐藏服务实现锁屏的方法

    Android编程获取系统隐藏服务实现锁屏的方法

    这篇文章主要介绍了Android编程获取系统隐藏服务实现锁屏的方法,涉及Android关于广播,服务,权限及锁屏等操作的相关技巧,需要的朋友可以参考下
    2015-12-12
  • Android OpenGLES如何给相机添加滤镜详解

    Android OpenGLES如何给相机添加滤镜详解

    这篇文章主要给大家介绍了关于Android OpenGLES如何给相机添加滤镜的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • Android中阻止AlertDialog关闭实例代码

    Android中阻止AlertDialog关闭实例代码

    这篇文章主要介绍了Android阻止AlertDialog关闭实例代码的相关资料,需要的朋友可以参考下
    2016-03-03
  • Android EventBus粘性事件实现机制探究

    Android EventBus粘性事件实现机制探究

    最近项目做组件化,需要进行组件化的通信,有时候可能会出现异步的情况,事件接收方还没准备好事件就已经发送过来了,这时候想到了EventBus的粘性事件,这篇文章主要给大家介绍了关于Android EventBus粘性事件实现机制的相关资料,需要的朋友可以参考下
    2022-05-05
  • Flutter与WebView通信方案示例详解

    Flutter与WebView通信方案示例详解

    这篇文章主要为大家介绍了Flutter与WebView通信方案示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android获取SHA1的方法

    Android获取SHA1的方法

    这篇文章主要介绍了Android获取SHA1的方法,需要的朋友可以参考下
    2017-12-12
  • Android编程之桌面小部件AppWidgetProvider用法示例

    Android编程之桌面小部件AppWidgetProvider用法示例

    这篇文章主要介绍了Android编程之桌面小部件AppWidgetProvider用法,结合具体实例形式分析了Android桌面组件AppWidgetProvider的功能、布局、权限设置等相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • Android Studio error: Unable to start the daemon process的解决方法

    Android Studio error: Unable to start the daemon process的解决方

    这篇文章主要介绍了在 Android Studio 上新建项目,出现 Unable to start the daemon process问题的几种的解决方法,需要的朋友可以参考下
    2020-10-10
  • Android room数据库使用详解

    Android room数据库使用详解

    这篇文章主要介绍了Android room数据库使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-11-11
  • Android自定义控件之翻转按钮的示例代码

    Android自定义控件之翻转按钮的示例代码

    本篇文章主要介绍了Android自定义控件之翻转按钮的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05

最新评论