安卓topbar编码实战
1.先在res->value下新建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="topBar">
<attr name="title" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
</declare-styleable>
</resources>
2.再编写组合布局,在layout下新建一个topbar.xml,左中右
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topbar_root" android:layout_width="match_parent"
android:layout_height="45dp"> <Button
android:text="left"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:id="@+id/topbar_leftbtn"
android:layout_width="wrap_content"
android:layout_height="match_parent" /> <TextView
android:id="@+id/topbar_tv"
android:textStyle="bold"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/topbar_rightbtn"
android:text="right"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
3.编写布局的加载类
package com.lingdangmao.demo_zidingyi_textview; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView; /**
* Created by Administrator on 2018/1/8.
*/ public class Topbar extends RelativeLayout { private Button topbarLeftBtn,topbarRightBtn;
private TextView topbarTextView;
private RelativeLayout topbar_root;
private int mColor= Color.BLUE;
private int mTextColor=Color.WHITE;
private String title; public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
//获得从外面加载的数据
initTypedArray(context,attrs);
//初始化页面
initView(context);
}
private void initTypedArray(Context context, AttributeSet attrs){
TypedArray ta =context.obtainStyledAttributes(attrs,R.styleable.topBar);
mTextColor = ta.getColor(R.styleable.topBar_titleTextColor,Color.WHITE);
title = ta.getString(R.styleable.topBar_title);
ta.recycle();
}
private void initView(Context context){
LayoutInflater.from(context).inflate(R.layout.topbar,this,true);
topbar_root =findViewById(R.id.topbar_root);
topbarLeftBtn = findViewById(R.id.topbar_leftbtn);
topbarRightBtn =findViewById(R.id.topbar_rightbtn);
topbarTextView =findViewById(R.id.topbar_tv); //设置背景颜色
topbar_root.setBackgroundColor(mColor);
//设置文字颜色
topbarTextView.setTextColor(mTextColor); //设置文字标题
setTitle(title);
//绑定事件
topbarLeftBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.d("ww", "onClick111: ");
listener.OnleftBtnClick();
}
});
} private void setTitle(String title){
if(!title.isEmpty()){
topbarTextView.setText(title);
}
}
private topbarOnClickListener listener; public interface topbarOnClickListener{
void OnleftBtnClick();
void OnRightBtnClick();
}
public void setTopbarClickListener(topbarOnClickListener listener){
this.listener=listener;
} public void setLeftOnClickListener(OnClickListener onClickListener){
topbarLeftBtn.setOnClickListener(onClickListener);
} }
4.在mainactivity中使用
package com.lingdangmao.demo_zidingyi_textview; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { private Topbar topbar;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topbar =findViewById(R.id.title); topbar.setTopbarClickListener(new Topbar.topbarOnClickListener() {
@Override
public void OnleftBtnClick() {
Log.d(TAG, "OnleftBtnClick: ");
}
@Override
public void OnRightBtnClick() { }
}); }
}
5.在主要布局中加载组合布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_root_ll"
tools:context="com.lingdangmao.demo_zidingyi_textview.MainActivity"> <com.lingdangmao.demo_zidingyi_textview.Topbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/title"
app:title="自定义组合控件"
app:titleTextSize="20dp"
app:titleTextColor="#ff0038"
android:layout_width="match_parent"
android:layout_height="45dp"> </com.lingdangmao.demo_zidingyi_textview.Topbar> </LinearLayout>
最后完成的效果下图

安卓topbar编码实战的更多相关文章
- 《Java8 Stream编码实战》正式推出
当我第一次在项目代码中看到Stream流的时候,心里不由得骂了一句"傻X"炫什么技.当我开始尝试在代码中使用Stream时,不由得感叹真香. 记得以前有朋友聊天说,他在代码中用了 ...
- 超详细的编码实战,让你的springboot应用识别图片中的行人、汽车、狗子、喵星人(JavaCV+YOLO4)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 小议安卓定位伪造-实战足不出户畅玩Pokemon Go
本文旨在技术探讨故本文不提供工具,正常玩家请勿模仿,游戏中虚拟位置有封号风险 0x00 安卓定位方式归类 要伪造定位首先要摸清定位到底是如何实现的,首先从广义上来区分安卓的定位方式实际上就gps和ne ...
- Web自动化之Headless Chrome编码实战
API 概览 && 编码Tips 文档地址 github Chrome DevTools Protocol 协议本身的仓库 有问题可以在这里提issue github debugger ...
- 安卓项目开发实战(1)--首页顶部菜单BAR实现
从今天開始,我将開始自己手写一个星座运势的项目,星座运势的数据来源採用MYAPI的星座数据,client全然自己实现. 这个系列主要是讲project中主要界面的布局展示和一些项目中的难点解析.因为本 ...
- 长篇图解etcd核心应用场景及编码实战
大家好啊,我是字母哥,今天写一篇关于etcd的文章,其实网上也有很多关于etcd的介绍,我就简明扼要,总结提炼,期望大家通过这一篇文章掌握etcd的核心知识以及编码技能! 本文首先用大白话给大家介绍一 ...
- 学习笔记_Java_day14—编码实战___一个注册页面的完整流程
- 编码实战Web端联系人的增删改查
首先画出分析图 实现效果如图 项目下的包如图: 实体包 package com.contactSystem.entiey; public class Contact { private String ...
- day14(编码实战-用户登录注册)
day14 案例:用户注册登录 要求:3层框架,使用验证码 功能分析 注册 登录 1.1 JSP页面 regist.jsp 注册表单:用户输入注册信息: 回显错误信息:当注册失败时,显示错误信 ...
随机推荐
- 字典树(Trie)的学习笔记
按照一本通往下学,学到吐血了... 例题1 字典树模板题吗. 先讲讲字典树: 给出代码(太简单了...)! #include<cstdio> #include<cstring> ...
- makefile = 与 := 的区别
“=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整个makefile中最后被指定的值.看例子: x = foo y = $(x) bar ...
- Java——static关键字---18.09.27
static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但在Java语言中没有全局变量的概念. static关键字主要有两种作用: 一.为某特定数据类 ...
- Migrating from MapReduce 1 (MRv1) to MapReduce 2 (MRv2, YARN)...
This is a guide to migrating from Apache MapReduce 1 (MRv1) to the Next Generation MapReduce (MRv2 o ...
- java基础 -- Collections.sort的两种用法
/** * @author * @version * 类说明 */ package com.jabberchina.test; import java.util.ArrayList; import j ...
- ubuntu12.04停留在grub界面问题
修改ubuntu 12.04 停留在grub界面的步骤: 1. 在/etc/default/grub配置文件中, 添加一项GRUB_RECORDFAIL_TIMEOUT: GRUB_TIMEOUT=2 ...
- 从源码安装opencv
操作系统为Debian9,由于使用apt-get安装在/usr/lib目录下的opencv可能会造成一些项目上的头文件错误问题,所以选择了从源码安装. 选择opencv3.4.1, 进入https:/ ...
- hibernate 批量插入
Session session = sessionFactoryUpLowLimit.openSession(); session.beginTransaction(); for(int i=0 ;i ...
- C++ XML文件解析
使用tinyxml2库,git地址https://github.com/leethomason/tinyxml2 只需要使用tinyxml2.h tinyxml2.cpp即可,同时需要using na ...
- c++知识点总结--函数模板
通用函数可变参模板 用于处理不限定参数的函数 showall(){//空函数,接口,最后结束递归 } template<typename T,typename... Args> void ...