完整项目下载

背景:项目中使用标题栏,只是简单的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. 【nginx】负载均衡和proxy的配置

    简介 使用upstream模块实现nginx负载均衡使用nginx_upstream_check_module模块实现后端服务器的健康检查使用nginx-sticky-module扩展模块实现Cook ...

  2. mysql插入数据与删除重复记录的几个例子(收藏)

    mysql插入数据与删除重复记录的几个例子 12-26shell脚本实现mysql数据的批量插入 12-26mysql循环语句插入数据的例子 12-26mysql批量插入数据(insert into ...

  3. cocos2d-x之xml文件读取初试

    auto doc=new tinyxml2::XMLDocument(); doc->Parse(FileUtils::getInstance()->getStringFromFile(& ...

  4. 关于mac环境下删除cocos2d-x环境变量配置的方法

    yangchaodeMacBook-Air:downloads yangchao$ vim ~/.bash_profile

  5. Tomcat 服务器版本的区别以及下载与安装

    Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开发而成.由于有了 ...

  6. 【node.js】安装express后,'express' 不是内部或外部命令的问题

    因express默认安装是最新的版本,已经是4.x.x的版本.而最新express4.0+版本中将命令工具分出来了,所以必须要安装express-generator,执行: D:\TOOLS\Node ...

  7. linux64位android项目R文件无法生成以及Cannot run program adb

    1.本机kali2.0  64位,kali基于Debian. 2.android adb是32位,64位linux要安装32位依赖库,注意ia32-lib被lib32z1替代. 3.执行命令 sudo ...

  8. Android Studio调试功能使用总结【转】

    Android Studio调试功能使用总结[转]   这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序. 1.设置断点 选定要设置断点的代码 ...

  9. NOIP2010提高组 关押罪犯 -SilverN

    (洛谷P1525) 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”( ...

  10. 使用jsonpath解析json内容

    JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容.下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它. 一.首先需要 ...