安卓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 注册表单:用户输入注册信息: 回显错误信息:当注册失败时,显示错误信 ...
随机推荐
- python 使用生成器 来完成 监听文件输入的例子
def tail(filename):#函数 f = open(filename,encoding='utf-8') while True: line = f.readline() if line.s ...
- R语言绘图:时间序列分析 ggplot2绘制ACF PACF
R语言真是博大精深 方法一 Acf(gold[,2], type = "correlation",lag.max = 100) Acf(gold[,2], type = " ...
- webDriver + Firefox 浏览器 完美兼容
搞java最烦的就是不同版本的适配问题.现分享下实测成功的案例. Firefox:4.0.1 selenium:selenium-server-standalone-2.43.1.jar 下面这个链接 ...
- .Net 面试题 汇总(三)
101.ASP.net的身份验证方式有哪些?分别是什么原理? 答:Windwos(默认)用IIS... From(窗体)用帐户 Passport(密钥) 102.在.net中,配件的意思是? 答:程序 ...
- CC3100BoosterPack和CC31XXEMUBOOST板子的测试
1. 先测试右边的CC3100BoosterPack,测试发现LDO坏了,无法输出3.3V,所以只能用左边的板子供电. 2. 插上CC31XXEMUBOOST板子的J1,两个板子插在一起,等待驱动安装 ...
- 常用js方法合集
var Default = { init: function () { }, addCookie: function (name,data) { var expdate = new Date(); / ...
- jmeter更改启动编码设置
项目中碰到这样的问题,在eclipse经过utf-8转码的代码,能正常运行,放到了jmeter里面运行,就是乱码,如下: String s = "乔佳飞"; String ss = ...
- IDEA的terminal设置成Linux的终端一样
方式一:通过在Windows上安装Linux命令行工具 前提:需要安装Linux终端的命令行工具,并且最好可以安装 Gow (一个Windows下模拟Linux命令行工具集合,它集成了 Liunx 环 ...
- 再见NullPointerException。在Kotlin里null的处理(KAD 19)
作者:Antonio Leiva 时间:Apr 4, 2017 原文链接:https://antonioleiva.com/nullity-kotlin/ 关于Kotlin最重要的部分之一:无效处理, ...
- python基础篇 05字典
本节主要内容:1. 字典的简单介绍2. 字典增删改查和其他操作3. 字典的嵌套 一. 字典的简单介绍:字典(dict)是python中唯一的一个映射类型.他是以{ }括起来的键值对组成. 在dict中 ...