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. Node.js中的不安全跳转如何防御详解

    Node.js中的不安全跳转如何防御详解 导语: 早年在浏览器大战期间,有远见的Chrome认为要运行现代Web应用,浏览器必须有一个性能非常强劲的Java引擎,于是Google自己开发了一个高性能的 ...

  2. centos 7 关闭IPtables

    systemctl status iptables.service systemctl stopiptables.service

  3. (数据科学学习手札08)系统聚类法的Python源码实现(与Python,R自带方法进行比较)

    聚类分析是数据挖掘方法中应用非常广泛的一项,而聚类分析根据其大体方法的不同又分为系统聚类和快速聚类,其中系统聚类的优点是可以很直观的得到聚类数不同时具体类中包括了哪些样本,而Python和R中都有直接 ...

  4. Linux下启动Oracle服务和监听程序步骤

    Linux下启动Oracle服务和监听程序启动和关闭步骤整理如下: 1.安装oracle: 2.创建oracle系统用户: 3./home/oracle下面的.bash_profile添加几个环境变量 ...

  5. LinqToExcel使用简介一

             最近才看到原来也可以用Linq来访问Excel,功能还挺强大的.要使用这个功能,首先得下载一个LinqToExcel的相关文件,然后就可以调用相关的方法.         使用前面介 ...

  6. 云计算之路-阿里云上:Web服务器请求到达量突降

    今天下午遇到了自使用阿里云以来首次遇到的新情况——http.sys的ArrivalRate突降(说明请求到达IIS的请求数量少了),而且SLB中的3台ECS都出现了这个问题. 1. 10.161.24 ...

  7. 商业地产 招商 招租 CRM 意向 洽谈 合同 复用商铺商户管理系统

    适用场合 本软件适合商业地产的对招商的全流程管理,包括商铺信息,商户信息,洽谈信息,意向签订,合同管理等. 软件有试用版可供下载试用. 联系方式 QQ:2417158658 Tel:130251102 ...

  8. Mac上配置Cocos2d-x开发环境(多平台:Android/iOS)

    下载以下资源: Cocos2d-x (http://www.cocos2d-x.org) Android NDK(http://developer.android.com/tools/sdk/ndk/ ...

  9. 2015年开源前端框架盘点TOP20

    2015年,榜单根据github上star数作为排名依据.(榜单中大部分为组件式框架, react.Angular等基础框架不在此篇讨论) 1.Bootstrap 类别/语言:HTML.CSS.Jav ...

  10. LeetCode 4——两个排序数组中的中位数

    1. 题目 2. 解答 2.1. 方法一 由于两个数组都是排好序的,因此首先可以想到的思路就是利用归并排序把两个数组合并成一个有序的长数组,然后直接取出中位数即可. class Solution: d ...