一、创建自定义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. 理解S12(X)架构中的地址映射方案

    目录 1. 介绍 2. CPU 本地地址 3. 分页窗口 4. 内存页 5. 控制各个对象在内存中放置的位置 介绍 在一个S12或S12X架构中,很有必要分清楚两种类型的内存地址:banked和non ...

  2. [ZJOI2008]树的统计——树链剖分

    本题是一个树链剖分裸题,由于比较菜,老是RE,后来发现是因为使用了全局变量. /************************************************************ ...

  3. 杭电oj2064、2067、2068、2073、2076-2078、2080、2083-2085

    2064  汉诺塔III #include<stdio.h> int main(){ int n,i; _int64 s[]; while(~scanf("%d",&a ...

  4. linux 安装 pip

    # wget https://bootstrap.pypa.io/get-pip.py # python get-pip.py

  5. VS2013 MFC C++ CString ,const char , char, string 类型转换

    VS2013 测试 以下测试加入头文件: # include <string>#include <cstdlib>using namespace std; //-------- ...

  6. urllib url解析学习

    #!/usr/bin/env python # encoding: utf-8 from urllib.parse import * #urlparse:解析url分段 #urlsplit:类似url ...

  7. ubuntu 安装TensorFlow

    1.安装pip $ sudo apt-get install python-pip python-dev 2.安装 TensorFlow for Python 2.7 # Ubuntu/Linux - ...

  8. [jquery] 给动态生成的元素绑定事件 on方法

    用底下的方法尝试了好多次都失败 $('.del').on('click',function(){ alert('aa'); })// 失败!! 终于在准备放弃前看到一篇博文说的方法 $(documen ...

  9. 二分LIS模板

    假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5. 下面一步一步试着找出它. 我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列. ...

  10. 洛谷 P1060 开心的金明【DP/01背包】

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就 ...