第一步:自定义xml属性

新建一个android项目,在values文件夹中新建一个atts.xml的文件,在这个xml文件中声明我们一会在使用自定义控件时候需要指明的属性。
atts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="ToolBar">
<attr name="title" format="string" />
<attr name="titleTextSize" format="dimension" />
<attr name="titleTextColor" format="color" />
<attr name="leftBackground" format="reference|color" />
<attr name="leftText" format="string" />
<attr name="leftTextColor" format="reference|color" />
<attr name="rightBackground" format="reference|color" />
<attr name="rightText" format="string" />
<attr name="rightTextColor" format="reference|color" />
</declare-styleable> </resources>

前面的name是我们要使用的属性名称,后面的format表示该属性接受的值的格式,string表示该属性的值是一个字符串,color表示该属性的值是一个颜色值,dimension表示该属性的值是一个尺寸,reference表示该属性的值可以参考某一个资源id,其他常见的format值还有:boolean(布尔值)、float(浮点值)、integer(整型值)、fraction(百分数)、enum(枚举值)、flag(位或运算)。


第二步:自定义标题类
在Java文件中自定义一个类继承RelativeLayout,并实现它的构造方法,我们的标题栏由三部分组成,左右两边各是一个Button,中间是一个TextView,因此我们在这个布局文件中要做的事就是对这三个控件进行处理。

先声明标题栏的三个空间及相关参数,这些参数都是根据atts.xml中来设置的,因为我们在引用自定义控件的时候是从xml中引用的,属性的设置都在xml文件中,我们从xml文件中拿到属性的值后再对控件设置赋值。

	/**
* 标题栏的三个控件
*/
private Button leftBtn, rightBtn;
private TextView title; /**
* 左边按钮的属性
*/
private int leftTextColor;
private Drawable leftBackground;
private String leftText; /**
* 右边按钮的属性
*/
private int rightTextColor;
private Drawable rightBackground;
private String rightText; /**
* 中间TextView的属性
*/
private int titleTextColor;
private String titleText;
private float titleTextSize; /**
* 三个控件的布局参数
*/
private LayoutParams leftParams, rightParams, titleParams;

下面是构造方法,构造方法传入两个参数,一个是上下文参数,另外一个是AttributeSet,AttributeSet是一个属性的集合,用它可以处理一组xml标签集合。使用ta.getXXX方法,我们可以先从xml文件获得属性的值,然后把这些值设置给控件。最后通过LayoutParams来设置控件的宽高,设置好宽高之后,调用addView方法,添加控件。

	public MyToolBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.ToolBar); leftTextColor = ta.getColor(R.styleable.ToolBar_leftTextColor, 0);
leftBackground = ta.getDrawable(R.styleable.ToolBar_leftBackground);
leftText = ta.getString(R.styleable.ToolBar_leftText); rightTextColor = ta.getColor(R.styleable.ToolBar_rightTextColor, 0);
rightBackground = ta.getDrawable(R.styleable.ToolBar_rightBackground);
rightText = ta.getString(R.styleable.ToolBar_rightText); titleText = ta.getString(R.styleable.ToolBar_title);
titleTextColor = ta.getColor(R.styleable.ToolBar_titleTextColor, 0);
titleTextSize = ta.getDimension(R.styleable.ToolBar_titleTextSize, 0); //对ta进行回收
ta.recycle(); leftBtn = new Button(context);
rightBtn = new Button(context);
title = new TextView(context); /**
* 设置属性
*/
leftBtn.setText(leftText);
leftBtn.setTextColor(leftTextColor);
leftBtn.setBackground(leftBackground); rightBtn.setText(rightText);
rightBtn.setTextColor(rightTextColor);
rightBtn.setBackground(rightBackground); title.setText(titleText);
title.setTextColor(titleTextColor);
title.setTextSize(titleTextSize);
title.setGravity(Gravity.CENTER); //设置整体背景颜色
setBackgroundColor(0x7CFC0055); leftParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
//添加控件
addView(leftBtn, leftParams); rightParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(rightBtn, rightParams); titleParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(title, titleParams); }

第三步:引用我们定义的控件
自定义好控件之后,我们就可以使用自定义的控件了,在主布局的xml文件中引用我们自定义的控件。自定义控件的前三个属性都是以android:开头,这表示这些属性都是系统的,后面的属性以custombar开头,表示这些属性都是我们自定义的,为了能够使用自定义的custombar,我们需要在RelativeLayout中添加一句:

xmlns:custombar="http://schemas.android.com/apk/res/com.example.mytoolbar"

注意后面的com.example.mytoolbar是你应用的包名称。如果阁下使用的不是eclipse而是android studio,那么这一行不用这么麻烦,只需要写上:

xmlns:custombar="http://schemas.android.com/apk/res-auto"

我们自定义的属性就是我们在atts.xml中声明的要设置的属性。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custombar="http://schemas.android.com/apk/res/com.example.mytoolbar"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <com.example.mytoolbar.MyToolBar
android:id="@+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="48dp"
custombar:leftBackground="@android:color/holo_blue_light"
custombar:leftText="返回"
custombar:leftTextColor="@android:color/black"
custombar:rightBackground="@android:color/holo_blue_light"
custombar:rightText="更多"
custombar:rightTextColor="@android:color/black"
custombar:title="标题栏"
custombar:titleTextColor="@android:color/black"
custombar:titleTextSize="18sp" >
</com.example.mytoolbar.MyToolBar> </RelativeLayout>

做完这些工作之后,运行你的项目,就能看到我们在文章开头给出的那个画面了。

第四步:为自定义控件添加事件

好像还少点什么,是的,我们的控件都还没有点击事件。要给事件设置点击事件,需要先在自定义控件中声明一个事件接口,并声明一个接口的实例:

private OnToolBarClickListener listener;
public interface OnToolBarClickListener {
/**
* 左边按钮点击事件
*/
public void leftClick(); /**
* 右边按钮点击事件
*/
public void rightClick();
}

然后暴露出来一个方法给其他类调用,这个方法的参数就是这个接口:

	public void setOnToolBarClickListener(OnToolBarClickListener listener) {
this.listener = listener;
}

最后在左右两个按钮的点击事件中调用接口中的方法即可,聪明的看官猜猜这是什么模式?

		leftBtn.setOnClickListener(new OnClickListener() {

			@Override
public void onClick(View v) {
listener.leftClick();
}
});
rightBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
listener.rightClick();
}
});

方法写好了,我们在MainActivity中调用看看:

public class MainActivity extends Activity {

	private MyToolBar toolBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
this.toolBar = (MyToolBar) this.findViewById(R.id.mytoolbar);
toolBar.setOnToolBarClickListener(new OnToolBarClickListener() { @Override
public void rightClick() {
Toast.makeText(MainActivity.this,"右边点击", Toast.LENGTH_LONG).show();
} @Override
public void leftClick() {
Toast.makeText(MainActivity.this,"左边点击", Toast.LENGTH_LONG).show();
}
});
}
} 以下是完整代码:
<?xml version="1.0" encoding="utf-8"?>
<resources> <!-- MyToolBar.java 自定义控件属性, -->
<declare-styleable name="MyToolBar">
<!-- 标题文字 -->
<attr name="mtitle" format="string" />
<!-- 标题文字大小 -->
<attr name="mtitleTextSize" format="dimension" />
<!-- 标题文字颜色 -->
<attr name="mtitleTextColor" format="color" /> <!-- format="reference|color" 可能是十六进制文件,也可能是drawable文件 -->
<attr name="mleftBackground" format="reference|color" />
<attr name="mleftText" format="string" />
<attr name="mleftTextColor" format="reference|color" /> <attr name="mrightBackground" format="reference|color" />
<attr name="mrightText" format="string" />
<attr name="mrightTextColor" format="reference|color" />
</declare-styleable> </resources>

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; public class MyToolBar extends RelativeLayout { /**
* 标题栏的三个控件
*/
private Button leftBtn, rightBtn;
private TextView title; /**
* 左边按钮的属性
*/
private int leftTextColor;
private Drawable leftBackground;
private String leftText; /**
* 右边按钮的属性
*/
private int rightTextColor;
private Drawable rightBackground;
private String rightText; /**
* 中间TextView的属性
*/
private int titleTextColor;
private String titleText;
private float titleTextSize; /**
* 三个控件的布局参数
*/
private LayoutParams leftParams, rightParams, titleParams; private OnToolBarClickListener listener; public interface OnToolBarClickListener {
/**
* 左边按钮点击事件
*/
public void leftClick(); /**
* 右边按钮点击事件
*/
public void rightClick();
} public void setOnToolBarClickListener(OnToolBarClickListener listener) {
this.listener = listener;
} public MyToolBar(Context context, AttributeSet attrs) {
super(context, attrs); // 通过 TypedArray 来获取在 atts.xml 自定义属性的值。
// ta 这里面就包含了所有自定义属性,值的映射。
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.MyToolBar); // 去除所有自定义属性值 赋值 给变量,然后通过这些变量赋值给控件使用。
// 自定义属性key的规定:属性名字_attr的名字
leftTextColor = ta.getColor(R.styleable.MyToolBar_mleftTextColor, 0);
leftBackground = ta.getDrawable(R.styleable.MyToolBar_mleftBackground);
leftText = ta.getString(R.styleable.MyToolBar_mleftText); rightTextColor = ta.getColor(R.styleable.MyToolBar_mrightTextColor, 0);
rightBackground = ta.getDrawable(R.styleable.MyToolBar_mrightBackground);
rightText = ta.getString(R.styleable.MyToolBar_mrightText); titleText = ta.getString(R.styleable.MyToolBar_mtitle);
titleTextColor = ta.getColor(R.styleable.MyToolBar_mtitleTextColor, 0);
titleTextSize = ta.getDimension(R.styleable.MyToolBar_mtitleTextSize, 0); //使用完后,一定要对ta进行回收。避免浪费资源和缓存的一些错误。
ta.recycle(); leftBtn = new Button(context);
rightBtn = new Button(context);
title = new TextView(context); /**
* 设置属性
*/
leftBtn.setText(leftText);
leftBtn.setTextColor(leftTextColor);
leftBtn.setBackground(leftBackground); rightBtn.setText(rightText);
rightBtn.setTextColor(rightTextColor);
rightBtn.setBackground(rightBackground); title.setText(titleText);
title.setTextColor(titleTextColor);
title.setTextSize(titleTextSize);
title.setGravity(Gravity.CENTER); // 标题设置居中显示 //设置整体背景颜色
setBackgroundColor(0x7CFC0055); leftParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
//添加控件
addView(leftBtn, leftParams); // 把控件 添加到Layout当中,并设置宽和高。
rightParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
// 定义控件的, 右对齐属性(这里的 TRUE 是一个常亮)。
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
// 添加到ViewGroup中去。
addView(rightBtn, rightParams); titleParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(title, titleParams); leftBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { listener.leftClick(); // 谁用谁实现,回调
}
}); rightBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { listener.rightClick(); // 谁用谁实现,回调
}
}); } public void setRightVisable(boolean flag) {
if (flag) {
rightBtn.setVisibility(View.VISIBLE);
} else {
rightBtn.setVisibility(View.GONE);
}
} }

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custombar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" > <com.dr.framework.mytoolbar.MyToolBar
android:id="@+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="48dp"
custombar:mleftBackground="@android:color/holo_blue_light"
custombar:mleftText="返回"
custombar:mleftTextColor="@android:color/black"
custombar:mrightBackground="@android:color/holo_blue_light"
custombar:mrightText="更多"
custombar:mrightTextColor="@android:color/black"
custombar:mtitle="标题栏"
custombar:mtitleTextColor="@android:color/black"
custombar:mtitleTextSize="10sp" >
</com.dr.framework.mytoolbar.MyToolBar> </RelativeLayout>
private MyToolBar toolBar;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.toolBar = (MyToolBar) this.findViewById(R.id.mytoolbar); toolBar.setOnToolBarClickListener(new OnToolBarClickListener() { @Override
public void rightClick() {
Toast.makeText(MainActivity.this, "右边点击", Toast.LENGTH_LONG).show();
} @Override
public void leftClick() {
Toast.makeText(MainActivity.this, "左边点击", Toast.LENGTH_LONG).show();
}
}); // toolBar.setRightVisable(false);
}
 

90、 Android UI模板设计的更多相关文章

  1. android ui界面设计参数讲解

    百度文库: http://wenku.baidu.com/link?url=s66Hw6byBEzmjL77doYL1YQN4Y_39F7MovaHKs5mVGrzTDOQCAmiM-1N_6Cdm- ...

  2. 怎样进行Android UI元素设计

    Android UI元素里面包含了许多的内容,比如:该平台由操作系统.中间件.用户界面和应用软件组成,一个应用程序要想受用户喜爱,那么UI可不能差. Android为相似的编程名词引入了一些新的术语, ...

  3. 详解 “Android UI”设计官方教程

    我们曾经给大家一个<MeeGo移动终端设备开发UI设计基础教程>,同时很多朋友都在寻找Android UI开发的教程,我们从Android的官方开发者博客找了一份幻灯片,介绍了一些Andr ...

  4. Android UI设计规则

    Android UI技巧 1.1 不该做什么 l  不要照搬你在其他平台的UI设计,应该让用户使用感觉是在真正使用一个Android软件,在你的LOGO显示和平台总体观感之间做好平衡 l  不要过度使 ...

  5. android自定义UI模板图文详解

    不知道大家在实际开发中有没有自定义过UI模板?今天花时间研究了一下android中自定义UI模板,与大家分享一下. 每个设计良好的App都是自定义标题栏,在自定义标题栏的过程中大部分人可能都是自定义一 ...

  6. shape和selector是Android UI设计中经常用到的

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

  7. Android UI(一)Layout 背景局部Shape圆角设计

    Jeff Lee blog:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...

  8. (转载)Android UI设计之AlertDialog弹窗控件

    Android UI设计之AlertDialog弹窗控件 作者:qq_27630169 字体:[增加 减小] 类型:转载 时间:2016-08-18我要评论 这篇文章主要为大家详细介绍了Android ...

  9. Android开发1:基本UI界面设计——布局和组件

    前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...

随机推荐

  1. Python内部类型

      Python使用对象模型来存储数据 . 身份:每个对象都有一个唯一的身份标识自己,任何对象的身份否可以使用内建函数id()来得到.这个值可以被认为是该对象的内存地址 . 类型:对象的类型决定了该对 ...

  2. mysql提权笔记

    最近小菜遇到mysql提权,总是会搞错,就记记笔记吧!以后方便用 先说手工吧! mysql<5.0,导出路径随意:5.0<=mysql<5.1,则需要导出至目标服务器的系统目录(如: ...

  3. 运行Appium碰到的坑们

    运行Appium的时候,碰到的那些坑 1. java命令会出现error:could not open ...jvm.cfg 出现这种情况大多是因为电脑上之前安装过JDK,卸载重装之后,运行java命 ...

  4. Maven 如何为不同的环境打包 —— 开发、测试和生产环境

    在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置,那每次 ...

  5. SpringQtz 时间任务调度

    1.配置所需要maven jar包 <!-- 任务调度需要的jar包--> <dependency> <groupId>org.quartz-scheduler&l ...

  6. eclipse项目里面的类有时候会莫名其妙出现很多错误

    由于eclipse的编译是基于时间戳的判断机制的.因此当你按build   all的时候有些eclipse认为时间戳没有改变的类不会被编译.因此你可以先clean一下再编译.这个时候eclipse会将 ...

  7. [原]Fedora 20安装记录

    Fedora是我最喜欢的Linux版本,很长时间以来我都在安装使用.近一年多以来一直在搞一个C#相关的开发,很久都没有接触Fedora了,我上一次使用的版本还是Fedora 17.本以为作为一个“老” ...

  8. Java ArrayList操作

    import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class Study { p ...

  9. 【linux】cut

    vim test id name age score 101 paul 18 100 102 suan 11 99 103 peter 18 98 [root@andon ~]# cut -f 2 t ...

  10. 51nod 1150 Logarithm

    题目来源: Ural 1318 给出n个互不相等的整数A[0] - A[n-1],选A[i]同A[j]进行异或运算(结果都 > 0无符号),对结果取lg(以10为底)并取整后记为L[i,j],求 ...