本文实例为大家分享了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 studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论