Android组合控件自定义标题栏
作者:Simple-Coder 时间:2021-11-04 01:12:36
本文实例为大家分享了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
标签:Android,标题栏
0
投稿
猜你喜欢
C#调用dll报错:无法加载dll,找不到指定模块的解决
2023-08-23 23:36:12
Java实现输出数字三角形实例代码
2023-08-25 02:09:51
Java实现在线五子棋对战游戏(人机对战)
2023-01-10 19:07:29
简单了解Java编程中抛出异常的方法
2023-08-07 17:31:49
使用注解@Recover优化丑陋的循环详解
2021-08-05 15:11:13
基于java实现租车管理系统
2022-02-08 12:48:49
详解Spring Boot Admin监控服务上下线邮件通知
2023-06-16 21:53:31
redis redisson 集合的使用案例(RList、Rset、RMap)
2023-11-13 13:50:21
Java中单例模式的7种写法
2021-09-05 23:40:57
Java lambda表达式与泛型整理总结
2021-07-15 19:57:14
Java emoji持久化mysql过程详解
2023-10-10 23:11:49
C#使用ToUpper()与ToLower()方法将字符串进行大小写转换的方法
2023-06-27 05:51:45
详解大数据处理引擎Flink内存管理
2023-03-09 14:41:43
关于Maven的使用,这些你都真的了解么
2022-01-02 14:19:08
Java多线程 ReentrantLock互斥锁详解
2022-07-23 21:21:06
C#异步编程Task的创建方式
2023-07-23 06:22:43
Mybatis配置之<typeAliases>别名配置元素解析
2023-08-02 03:09:54
使用java技术抓取网站上彩票双色球信息详解
2022-01-04 14:47:51
maven profile动态选择配置文件详解
2023-11-05 22:05:20
C# 标准事件流实例代码
2022-06-21 16:29:14