一、创建自定义TopBar头部菜单条

  实现步骤:

  1、在values中添加attrs.xml文件,设置自定义属性。

  2、添加Topbar类,继承RelativeLayout,实现具体功能。

  3、添加到页面上,并设置添加事件。

参考代码:

  values\attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<attr name="toptitle" format="string" /> <!--中间文字,类型字符串-->
<attr name="titleTextSize" format="dimension" /> <!--字体大小,类型为数字-->
<attr name="titleTextColor" format="color"/> <!--字体颜色,类型为颜色-->
<attr name="leftTextColor" format="color"/> <!--左侧字体颜色,类型为颜色-->
<attr name="leftBackground" format="reference|color" /> <!--左侧背景颜色,类型为图片和颜色-->
<attr name="leftText" format="string" /> <!--左侧文字-->
<attr name="rightTextColor" format="color"/> <!--右侧文字颜色-->
<attr name="rightBackground" format="reference|color" /> <!--右侧背景-->
<attr name="rightText" format="string" /> <!--右侧文字-->
</declare-styleable>
</resources>

  TopBar.java,自定义View实现类。

package com.example.zhengcheng.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView; /**
* Created by zhengcheng on 2015/4/11.
*/
public class TopBar extends RelativeLayout {
private Button btn_left, btn_right;
private TextView tv_title; private int leftTextColor;
private Drawable leftBackground;
private String leftText; private int rightTextColor;
private Drawable rightBackground;
private String rightText; private float titleTextSize;
private int titleTextColor;
private String toptitle; //定义三个布局参数
private LayoutParams leftParams, rightParams, titleParams; //定义一个事件接口
public interface topbarClickListener{
public void leftClick();
public void rightClick();
} //创建接口对象
public topbarClickListener listener; //创建为事件接口赋值的方法
public void setOnTopBarClickListener(topbarClickListener listener){
this.listener = listener;
} //构造方法,初始化成员
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs); //将XML中定义的自定义属性映射到attrs中。
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar); //从ta结构中获取数据,类似一种key,value结构,通过R.styleable.Topbar_属性名获取
leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
leftText = ta.getString(R.styleable.Topbar_leftText); rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
rightText = ta.getString(R.styleable.Topbar_rightText); titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
toptitle = ta.getString(R.styleable.Topbar_toptitle); //进行垃圾回收
ta.recycle(); //初始化控件
btn_left = new Button(context);
btn_right = new Button(context);
tv_title = new TextView(context); //设置控件的值
btn_left.setTextColor(leftTextColor); //设置文字颜色
btn_left.setBackground(leftBackground); //设置背景
btn_left.setText(leftText); //设置文本 btn_right.setTextColor(rightTextColor); //设置文字颜色
btn_right.setBackground(rightBackground); //设置背景
btn_right.setText(rightText); //设置文本 tv_title.setTextColor(titleTextColor); //设置字体颜色
tv_title.setTextSize(titleTextSize); //设置字体大小
tv_title.setText(toptitle); //设置文本
tv_title.setGravity(Gravity.CENTER); //居中显示 setBackgroundColor(0xfff59563); //设置View的背景颜色 //设置布局属性的width和height
leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//设置对齐方式为父容器的左侧
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
//将左边按钮添加到视图中,并设置布局属性
addView(btn_left, leftParams); //设置布局属性的width和height
rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//设置对齐方式为父容器的右侧
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
//将右边按钮添加到视图中,并设置布局属性
addView(btn_right, rightParams); //设置布局属性的width和height
titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//设置对齐方式为居中对齐
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
//将中间TextView添加到视图中,并设置布局属性
addView(tv_title, titleParams); //添加左侧按钮的Click事件
btn_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
}
}); //添加右侧按钮的Click事件
btn_right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
}
});
} /**
* 设置左边按钮是否隐藏,true隐藏, false消失
* @param flag
*/
public void setLeftButtonIsVisiable(boolean flag){
if(flag){
btn_left.setVisibility(View.VISIBLE);
}else{
btn_left.setVisibility(View.GONE);
}
} /**
* 设置右边按钮是否隐藏,true隐藏, false消失
* @param flag
*/
public void setRightButtonIsVisiable(boolean flag){
if(flag){
btn_right.setVisibility(View.VISIBLE);
}else{
btn_right.setVisibility(View.GONE);
}
}
}

  main.xml,主页面文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"> <com.example.zhengcheng.myapplication.TopBar
android:id="@+id/MyTopbar"
android:layout_width="match_parent"
android:layout_height="40dp"
custom:leftTextColor="#FFFFFF"
custom:leftText="Back"
custom:leftBackground="#ffa4c161"
custom:rightTextColor="#FFFFFF"
custom:rightText="More"
custom:rightBackground="#ffa4c161"
custom:titleTextSize="8dp"
custom:titleTextColor="#000000"
custom:toptitle="自定义模版">
</com.example.zhengcheng.myapplication.TopBar>
</RelativeLayout>

  main.java 后台代码文件

package com.example.zhengcheng.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TopBar topbar = (TopBar) findViewById(R.id.MyTopbar); //设置左右按钮为隐藏
topbar.setLeftButtonIsVisiable(false);
topbar.setRightButtonIsVisiable(false); //添加topbar的事件
topbar.setOnTopBarClickListener(new TopBar.topbarClickListener() {
@Override
public void leftClick() {
Toast.makeText(MainActivity.this,"点击了左边的按钮",Toast.LENGTH_SHORT).show();
} @Override
public void rightClick() {
Toast.makeText(MainActivity.this,"点击了右边的按钮",Toast.LENGTH_SHORT).show();
}
});
}
}

  全部功能实现,可以使某个功能模块重复利用。大大提高代码的福永率,有点类似.net中的用户控件!

Android学习(十七)自定义View控件 TopBar的更多相关文章

  1. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  2. Android开发学习笔记-自定义组合控件的过程

    自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...

  3. android学习日记03--常用控件button/imagebutton

    常用控件 控件是对数据和方法的封装.控件可以有自己的属性和方法.属性是控件数据的简单访问者.方法则是控件的一些简单而可见的功能.所有控件都是继承View类 介绍android原生提供几种常用的控件bu ...

  4. Android 手机卫士--自定义组合控件构件布局结构

    由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...

  5. Android开发之自定义组合控件

    自定义组合控件的步骤1.自定义一个View,继承ViewGroup,比如RelativeLayout2.编写组合控件的布局文件,在自定义的view中加载(使用View.inflate())3.自定义属 ...

  6. [android] 手机卫士自定义组合控件

    设置中心 新建SettingActivity 设置GridView条目的点击事件 调用GridView对象的setOnItemClickListenner()方法,参数:OnItemClickList ...

  7. Android自定义控件之自定义组合控件(三)

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  8. Android开发学习笔记-自定义组合控件

    为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...

  9. Android自定义view控件

    转载自: http://blog.163.com/ppy2790@126/blog/static/103242241201382210910473/ 开发自定义控件的步骤: 1.了解View的工作原理 ...

随机推荐

  1. JS 自定义时间格式化

    // 对Date的扩展,将 Date 转化为指定格式的String// 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位 ...

  2. 莫队 [洛谷2709] 小B的询问[洛谷1903]【模板】分块/带修改莫队(数颜色)

    莫队--------一个优雅的暴力 莫队是一个可以在O(n√n)内求出绝大部分无修改的离线的区间问题的答案(只要问题满足转移是O(1)的)即你已知区间[l,r]的解,能在O(1)的时间内求出[l-1, ...

  3. cookie登录

    #coding:utf-8 import tornado.httpserver import tornado.ioloop import tornado.options import tornado. ...

  4. 关于boostrapValidator动态添加字段(addField)验证的bug

    每次码博客,都觉得自己怀才不遇,哎~脑袋有瑕疵,文笔拿不粗手,就直接上干货吧. 在使用boostrapValidator这个验证插件的时候,如果某一个字段是动态添加来的,我们需要调用方法:addFie ...

  5. 【linux高级程序设计】(第十章)Linux异步信号处理机制 3

    信号屏蔽 信号忽略:系统仍然传递该信号,只是相应的进程不做任何处理 信号屏蔽:进程不捕获信号,信号处于未决状态,当不再屏蔽信号时可以捕获之前被屏蔽的信号. 信号集数据结构定义: typedef __s ...

  6. cmake add_executable 与 include_directories

    在cmake里add_executable里如果没有包含.cpp文件,该.cpp文件就不适用include_directories包含文件

  7. Manacher【p4555】 [国家集训队]最长双回文串

    题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n 的串 S ,求 S 的最长双回文子串 T ,即可 ...

  8. 一个错误使用单例模式的场景及ThreadLocal简析

    近来参与一个Java的web办公系统,碰到一个bug,开始猜测是线程池管理的问题,最后发现是单例模式的问题. 即,当同时发起两个事务请求时,当一个事务完成后,另一个事务会抛出session is cl ...

  9. Linux/Unix面试题

    shell中如何改变文件中的某个关键字 unix命令 unix shell中在特定文件夹内查找包含指定字符串的文件用哪个命令 如何用要shell找到指定目录下的最近一天更新的文件,要包含子目录 Lin ...

  10. -------------》》》【Vim】vim的一些简单配置~

    在linux下使用   :vsp ~/.vimrc   来打开配置文件 一些命令[任何命令前加上   !    强制执行]: :w     保存 :wq    保存退出 :q     退出 ctrl ...