完整项目下载

背景:项目中使用标题栏,只是简单的include一个标题栏的视图,赋值、控制元素显示、点击事件都要自己搞,不优雅!

要求:

1:对现有代码入侵最小

2:使用足够简单

OK,围绕着这个需求,咱做了一个标准的标题栏。中间有文本,左右两边可以是文字或者是图片。

显示标题栏和左侧文字的调用方式如下:

 <zhexian.app.myapplication.ActionBarLeftRightButton
android:layout_width="match_parent"
android:layout_height="50dp"
app:titleLeftImage="@mipmap/arrow_back"
app:titleRightText="提交"
app:titleText="当春乃发生"/>

后台事件呢:控制元素隐藏,设置点击事件?

答案是一句都没有。66666666

怎么实现的?

咱设置了一个Style,文字优先级比图片高。你设置哪个属性,哪个属性对应的控件就会显示。

本控件会判断载体Context是否实现了click事件,如果是,自动给显示的控件加上OnClick事件。

 <declare-styleable name="ActionBarLeftRightButton">
<attr name="titleLeftText" format="string"/>
<attr name="titleLeftImage" format="reference"/>
<attr name="titleText" format="string"/>
<attr name="titleRightText" format="string"/>
<attr name="titleRightImage" format="reference"/>
</declare-styleable>

所以后台你要做的全部,就是在OnClick事件里面写对应ID的实现就好了。

实际用下来之后,体验比以前好多了,优雅~

核心代码如下:

package zhexian.app.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView; /**
* 标题栏,左右有按钮,图片或者文字都可以
* 优先文字、其次图片,两者都没有的话则不显示
* 参考属性R.styleable.ActionBarLeftRightButton
* Created by 陈俊杰 on 2015/12/8.
*/
public class ActionBarLeftRightButton extends RelativeLayout {
public ActionBarLeftRightButton(Context context) {
this(context, null, 0);
} public ActionBarLeftRightButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public ActionBarLeftRightButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
} /**
* 如果需要对具体的某个元素进行单独操作,可以用本函数获得该对象的引用
*
* @param id
* @param <T>
* @return
*/
public <T> T getView(int id) {
return (T) findViewById(id);
} /**
* 初始化,需要Context实现了OnClickListener
*
* @param context
* @param attrs
*/
void initView(Context context, AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.view_actionbar_left_right_btn, this, true);
setBackgroundResource(R.color.orange);
TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.ActionBarLeftRightButton); boolean isHolderCanClick = context instanceof OnClickListener;
OnClickListener onClickListener = isHolderCanClick ? (OnClickListener) context : null; bindNavigateAction(view, true, attrArray, onClickListener);
bindNavigateAction(view, false, attrArray, onClickListener); String titleText = attrArray.getString(R.styleable.ActionBarLeftRightButton_titleText); if (!TextUtils.isEmpty(titleText))
bindTextView((TextView) view.findViewById(R.id.title_text), titleText, onClickListener); attrArray.recycle();
} /**
* 绑定左边或者右边的按钮、文字
*
* @param view
* @param isLeft
* @param attrArray
* @param onClickListener
*/
void bindNavigateAction(View view, boolean isLeft, TypedArray attrArray, OnClickListener onClickListener) {
String leftText = attrArray.getString(isLeft ? R.styleable.ActionBarLeftRightButton_titleLeftText : R.styleable.ActionBarLeftRightButton_titleRightText); if (!TextUtils.isEmpty(leftText)) {
bindTextView(view, leftText, isLeft, onClickListener); } else {
Drawable leftImage = attrArray.getDrawable(isLeft ? R.styleable.ActionBarLeftRightButton_titleLeftImage : R.styleable.ActionBarLeftRightButton_titleRightImage); if (leftImage != null)
bindImageView(view, leftImage, isLeft, onClickListener);
}
} void bindTextView(View view, String text, boolean isLeft, OnClickListener onClickListener) {
bindTextView((TextView) view.findViewById(isLeft ? R.id.title_left_text : R.id.title_right_text), text, onClickListener);
} /**
* 绑定文本
*
* @param textView
* @param text
* @param onClickListener
*/
void bindTextView(TextView textView, String text, OnClickListener onClickListener) {
textView.setVisibility(VISIBLE);
textView.setText(text); if (onClickListener != null)
textView.setOnClickListener(onClickListener);
} /**
* 绑定图片
*
* @param view
* @param drawable
* @param isLeft
* @param onClickListener
*/
void bindImageView(View view, Drawable drawable, boolean isLeft, OnClickListener onClickListener) {
ImageButton button = (ImageButton) view.findViewById(isLeft ? R.id.title_left_button : R.id.title_right_button);
button.setVisibility(VISIBLE);
button.setImageDrawable(drawable); if (onClickListener != null)
button.setOnClickListener(onClickListener);
}
}

【android】实现一个自己的标题栏的更多相关文章

  1. Android沉浸式(侵入式)标题栏(状态栏)Status(二)

     Android沉浸式(侵入式)标题栏(状态栏)Status(二) 附录1以xml写style实现了Android沉浸式(侵入式)状态栏(标题栏),同样以上层Java代码实现.在附录文章1的基础上 ...

  2. Android沉浸式(侵入式)标题栏(状态栏)Status(一)

     Android沉浸式(侵入式)标题栏(状态栏)Status(一) 现在越来越多的APP设计采用这种称之为沉浸式状态栏(Status)的设计,这种沉浸式状态栏又称之"侵入式"状 ...

  3. Android:一个高效的UI才是一个拉风的UI(二)

    趁今晚老大不在偷偷早下班,所以有时间继续跟大伙扯扯UI设计之痛,也算一个是对上篇<Android:一个高效的UI才是一个拉风的UI(一)>的完整补充吧.写得不好的话大家尽管拍砖~(来!砸死 ...

  4. Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect)

    Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect) [TOC] 这两个方法的区别 View.ge ...

  5. Android由一个activity 间隔5秒自动跳转到另外一个activity

    Android由一个activity 间隔5秒自动跳转到另外一个activity 2013年10月10日18:03:42 //一.写一个定时器 5秒后开启        final Intent lo ...

  6. Android沉浸式(侵入式)标题栏(状态栏)Status(三)

     Android沉浸式(侵入式)标题栏(状态栏)Status(三) 从附录文章1,2可以看到,依靠Android系统提供的标准方案,状态栏即便在透明状态下,仍然有些半透明而不是全透明.本文是And ...

  7. Android:一个高效的UI才是一个拉风的UI(一)

    开篇 Android是一个运行在移动终端上的操作系统,跟传统PC最大的不同所在就是移动终端的资源紧缺问题“比较”明显,当然对于一些屌丝机型,应该用“非常“来形容才靠谱.所以经常会出现在一些比较缺乏青春 ...

  8. android 让一个控件按钮居于底部的几种方法

    android 让一个控件按钮居于底部的几种方法1.采用linearlayout布局:android:layout_height="0dp" <!-- 这里不能设置fill_ ...

  9. Android中一个类实现的接口数不能超过七个

    近期一段时间,在开发Android应用程序的过程中,发现Android中一个类实现的接口数超过七个的时候,常常会出现超过第7个之后的接口不能正常使用.

随机推荐

  1. hdu 4738 Caocao's Bridges 图--桥的判断模板

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 3374 String Problem (KMP+最大最小表示)

    KMP,在有循环节的前提下: 循环节 t = len-next[len], 个数num = len/(len-next[len]);个人理解,如果有循环节,循环节长度必定小于等于len/2, 换句话说 ...

  3. java charset detector

    https://code.google.com/p/juniversalchardet/downloads/list java移植mozilla的编码自动检测库(源码为c++),准确率高. 通过svn ...

  4. openfire+asmack搭建的安卓即时通讯(二) 15.4.9

    上期没有放成果图呢!忘了=-=,这就是上次的成果图,textview里面会显示登陆的名字(这个是默认管理员帐号=-=) 好吧,登陆了服务器我们就有了交互的功能啦可以说是前进了一大步呢!下面能我们就要试 ...

  5. openPOWERLINK官方安装版例程(v2.3.0)附带mnobd.cdc文件断句

    demo_mn_qt.exe启动所需载入的mnobd.cdc文件断句(备忘) //// Project: Demo_3CN //// NodeCount: 3 //// 0000003A //// N ...

  6. mysql 防止update/delete误操作

    身为一php开发攻城狮,常常涉及在应用中写update/delete语句,忘记加where,后果不堪设想. 还会出现在cml下直接操作mysql的情况,如果mysql 权限够大,一个update/de ...

  7. 原创翻译-值得关注的10个python语言博客

    原文链接 原文链接的网页感觉网络不是很好,不容易上.我在这里就给大家做个翻译吧. 大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建 ...

  8. POJ 3264 Balanced Lineup -- RMQ或线段树

    一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...

  9. 用js判断时间的先后顺序

    我们在用户注册信息的时候,有时根据需要往往要加入一些时间上的判断,今天我在这里给大家推荐一款比较实用的时间先后顺序判断的代码,希望对大家有所有帮助. <!DOCTYPE HTML> < ...

  10. svn命令行修改已提交的版本备注

    svn命令行修改已提交的版本备注 参考文章: stackoverflow.com/questions/304383/how-do-i-edit-a-log-message-that-i-already ...