Android组合控件自定义标题栏
作者:Simple-Coder 发布时间:2021-11-04 01:12:36
标签: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>
粘贴以上代码就可以运行了。
来源:https://blog.csdn.net/qq_32895969/article/details/56845024


猜你喜欢
- 在使用C#进行相关编程的时候,有时候我们需要获取系统相关的进程信息。那么在C#中如何获取系统的所有进程那?下面请跟小编一起来操作。1、首先新
- java 多线程死锁 相信有过多线程编程经验的朋友,都吃过死锁的苦。除非你不使用多线程,否则死锁的可能性会一直存在。为什么会出现
- 在看KMP算法时,想要简单的统计一下执行时间和性能。得出的结论是: Java的String的indexOf方法性能最好,其次是KMP算法,其
- 目录一、需求二、步骤三、结果一、需求把以下txt中含“baidu”字符串的链接输出到一个文件,否则输出到另外一个文件。二、步骤1.LogMa
- 多级缓存在实际开发项目,为了减少数据库的访问压力,都会将数据缓存到内存中比如:Redis(分布式缓存)、EHCHE(JVM内置缓存).例如在
- package com;import java.util.Arrays; public class sjf { &nbs
- 一、需求有时候应用需要在内部切换语言但又不影响系统的语言,比如是应用现在是中文的,系统语言也是中文的,我把应用的切换成英文显示后系统语言还是
- 记录一下使用IDEA创建servlet并使用Tomcat本地部署的过程。需要安装好的软件首先IDEA社区版不支持Java EE,因此要使用U
- 拆分字符串:这个可以使用两次分割,第一次使用 | 分割,放到arr数组里,然后使用循环对arr[i]进行使用:分割public static
- 介绍开发中,页面头部为搜索样式的设计非常常见,为了可以像系统AppBar那样使用,这篇文章记录下在Flutter中自定义一个通用的搜索框Ap
- 这篇文章主要介绍了JAVA利用递归删除文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 前言Java17将是一个长期支持的LTS版本。Java采用了6个月的发布周期。也就是说,它将每6个月发布一个新版本的Java。每隔3年,LT
- 本文实例为大家分享了java实现TCPSocket聊天室功能的相关代码,供大家参考,具体内容如下1.TCPserver.javaimport
- 本文实例讲述了Android实现基于滑动的SQLite数据分页加载技术。分享给大家供大家参考,具体如下:main.xml如下:<men
- struct OutputFilestruct OutputFile 是单个输出文件的管理器。之前在 parse_opt
- 这篇文章主要介绍了java通过实例了解值传递和引用传递,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 一、同步问题提出线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏。例如:两个线程ThreadA、ThreadB都操作同一个对
- 本文实例为大家分享了Rxjava实现轮询定时器的具体代码,供大家参考,具体内容如下作用1、实现了延迟若干毫秒后,执行next操作,只执行一次
- 由于公司平台访问人数逐渐增多,公司项目的数据库已经几次出现宕机现象。为减轻数据库压力,我上个月对公司项目做了下调整。把新闻板块提取出来单独一
- 如果你想知道java annotation是什么?你可以先看看:“http://www.infoq.com/articles/Annotat