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编码实战的更多相关文章

  1. 《Java8 Stream编码实战》正式推出

    ​当我第一次在项目代码中看到Stream流的时候,心里不由得骂了一句"傻X"炫什么技.当我开始尝试在代码中使用Stream时,不由得感叹真香. 记得以前有朋友聊天说,他在代码中用了 ...

  2. 超详细的编码实战,让你的springboot应用识别图片中的行人、汽车、狗子、喵星人(JavaCV+YOLO4)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. 小议安卓定位伪造-实战足不出户畅玩Pokemon Go

    本文旨在技术探讨故本文不提供工具,正常玩家请勿模仿,游戏中虚拟位置有封号风险 0x00 安卓定位方式归类 要伪造定位首先要摸清定位到底是如何实现的,首先从广义上来区分安卓的定位方式实际上就gps和ne ...

  4. Web自动化之Headless Chrome编码实战

    API 概览 && 编码Tips 文档地址 github Chrome DevTools Protocol 协议本身的仓库 有问题可以在这里提issue github debugger ...

  5. 安卓项目开发实战(1)--首页顶部菜单BAR实现

    从今天開始,我将開始自己手写一个星座运势的项目,星座运势的数据来源採用MYAPI的星座数据,client全然自己实现. 这个系列主要是讲project中主要界面的布局展示和一些项目中的难点解析.因为本 ...

  6. 长篇图解etcd核心应用场景及编码实战

    大家好啊,我是字母哥,今天写一篇关于etcd的文章,其实网上也有很多关于etcd的介绍,我就简明扼要,总结提炼,期望大家通过这一篇文章掌握etcd的核心知识以及编码技能! 本文首先用大白话给大家介绍一 ...

  7. 学习笔记_Java_day14—编码实战___一个注册页面的完整流程

  8. 编码实战Web端联系人的增删改查

    首先画出分析图 实现效果如图 项目下的包如图: 实体包 package com.contactSystem.entiey; public class Contact { private String ...

  9. day14(编码实战-用户登录注册)

    day14 案例:用户注册登录 要求:3层框架,使用验证码   功能分析 注册 登录   1.1 JSP页面 regist.jsp 注册表单:用户输入注册信息: 回显错误信息:当注册失败时,显示错误信 ...

随机推荐

  1. ruby require的使用

    引用单个文件 例: 引用当前rb同目录下的file_to_require.rb先介绍3种方法 require File.join(__FILE__, '../file_to_require') req ...

  2. go学习笔记-运算符

    运算符 运算符 内置运算符 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 算术运算符 假定 A 值为 10,B 值为 20. 运算符 描述 实例 + 相加 A + B 输出结果 ...

  3. Kubernetes-Service Account

    kube-apiserver 配置文件:/etc/kubernetes/apiserver KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0 ...

  4. (数据科学学习手札05)Python与R数据读入存出方式的总结与比较

    在数据分析的过程中,外部数据的导入和数据的导出是非常关键的部分,而Python和R在这方面大同小异,且针对不同的包或模块,对应着不同的函数来完成这部分功能: Python 1.TXT文件 导入: 以某 ...

  5. linux execl()函数

    关于execl()函数族的用法不在赘述,其他博主介绍的很详细.下面说下作者在使用该函数时所犯的错误: 作者想通过使用execl()函数在子进程中调用其他函数,起初楼主是 这样用的: if((a = e ...

  6. myeclipse 安装pydev插件后svn插件失效

    为了将python的IDE集成到myeclipse,按照教程安装了myeclipse插件pydev插件,但是按照完后发现,先前安装的svn不见了,解决办法如下: 1. 关闭myeclipse, 2. ...

  7. 新版IdFTP解决中文乱码问题

    用XE10后开发FTP客户端,发现有中文乱码问题.这里也主要是编码的问题,在connect链接后,需要设置编码方可. 注意:  IndyTextEncoding_OSDefault;   该代码可能需 ...

  8. 利尔达CC3200模块烧写程序笔记

    1. 硬件使用利尔达的CC3200模块,仿真下载器使用利尔达的FTDI仿真器,硬件完全兼容官方的仿真器.仿真器支持IAR的调试,单步运行等操作. 2. 硬件连接接线说明: RXD, TXD, GNG, ...

  9. java堆内存模型

     广泛地说,JVM堆内存被分为两部分——年轻代(Young Generation)和老年代(Old Generation). 年轻代 年轻代是所有新对象产生的地方.当年轻代内存空间被用完时,就会触发垃 ...

  10. asp.net webapi 使用小结

    一星期前公司用webapi处理一些事情,自己总结一下用法. 1.创建一个空的webapi会默认有一下几个方法. public class ValueController : ApiController ...