安卓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 注册表单:用户输入注册信息: 回显错误信息:当注册失败时,显示错误信 ...
随机推荐
- 毕业2年 Summary
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/135 看了下去年写毕业一周年总结的时间:2017-6-16,今天 ...
- 动态规划(DP)算法
参考https://blog.csdn.net/libosbo/article/details/80038549 动态规划是求解决策过程最优化的数学方法.利用各个阶段之间的关系,逐个求解,最终求得全局 ...
- sudo mount -o loop pm.img /mnt/floppy
sudo mount -o loop pm.img /mnt/floppy 最近在学<一个操作系统的实现>,由于这本书比较老了,所以有一些对于软盘的操作指令现在用会出现一些错误,当我进行虚 ...
- xss挑战赛小记 0x03(xssgame)
0x00 继续做xss吧 这次是xssgame 地址 http://www.xssgame.com/ 一共八关 学到了很多东西 0x01 啥也没有 <svg/onload="alert ...
- Django学习之天气调查实例(2):显示数据表数据
数据表数据添加后,如添加3条用户信息,分别为“aaa”.“bbb”.“ccc”,现在通过代码的方式显示数据表中的数据. 1.在website项目文件夹中创建 userload.py文件,并且写如下代码 ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- C#导出数据到CSV和EXCEL文件时数字文本被转义的解决方法
今天写C#导出datagrid数据到csv格式文件的时候,发现不管怎么尝试,凡是单元格里面全是数字的单元格,在用Excel打开的时候,都被自动转义成数据格式.数据查看极其不方便.最后google了一下 ...
- 使用PSSH批量操作Linux服务器
简介 服务器多了,有一个问题就是如何批量快速操作多台服务器,在网上搜到了PSSH工具,试用了一下发现挺好用,推荐给大家. pssh是一个python编写的可以在多台服务器上执行命令的轻量级管理工具,同 ...
- Vue一些重要的知识点
vue sync修饰(1)双向数据绑定,父子组件之间信息的交互 1⃣️在自组件中使用this.emmit('toFather'),子组件产生一个tofather事件,然后在父组件中通过@进行监听,那么 ...
- 详说大数据计算的可类化Classable
可类化(Classable)是Laxcus大数据管理系统提供的一项基础功能,它能够将类转化为一串字节数组,或者逆向将字节数组转化为一个类.这项功能与JAVA提供的序列化(Serializable)非常 ...