【android】实现一个自己的标题栏
背景:项目中使用标题栏,只是简单的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】实现一个自己的标题栏的更多相关文章
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
Android沉浸式(侵入式)标题栏(状态栏)Status(二) 附录1以xml写style实现了Android沉浸式(侵入式)状态栏(标题栏),同样以上层Java代码实现.在附录文章1的基础上 ...
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
Android沉浸式(侵入式)标题栏(状态栏)Status(一) 现在越来越多的APP设计采用这种称之为沉浸式状态栏(Status)的设计,这种沉浸式状态栏又称之"侵入式"状 ...
- Android:一个高效的UI才是一个拉风的UI(二)
趁今晚老大不在偷偷早下班,所以有时间继续跟大伙扯扯UI设计之痛,也算一个是对上篇<Android:一个高效的UI才是一个拉风的UI(一)>的完整补充吧.写得不好的话大家尽管拍砖~(来!砸死 ...
- Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect)
Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect) [TOC] 这两个方法的区别 View.ge ...
- Android由一个activity 间隔5秒自动跳转到另外一个activity
Android由一个activity 间隔5秒自动跳转到另外一个activity 2013年10月10日18:03:42 //一.写一个定时器 5秒后开启 final Intent lo ...
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
Android沉浸式(侵入式)标题栏(状态栏)Status(三) 从附录文章1,2可以看到,依靠Android系统提供的标准方案,状态栏即便在透明状态下,仍然有些半透明而不是全透明.本文是And ...
- Android:一个高效的UI才是一个拉风的UI(一)
开篇 Android是一个运行在移动终端上的操作系统,跟传统PC最大的不同所在就是移动终端的资源紧缺问题“比较”明显,当然对于一些屌丝机型,应该用“非常“来形容才靠谱.所以经常会出现在一些比较缺乏青春 ...
- android 让一个控件按钮居于底部的几种方法
android 让一个控件按钮居于底部的几种方法1.采用linearlayout布局:android:layout_height="0dp" <!-- 这里不能设置fill_ ...
- Android中一个类实现的接口数不能超过七个
近期一段时间,在开发Android应用程序的过程中,发现Android中一个类实现的接口数超过七个的时候,常常会出现超过第7个之后的接口不能正常使用.
随机推荐
- MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数
安装MySQL时,有warning: [root@localhost mysql]# scripts/mysql_install_db --user=mysql Installing MySQL sy ...
- c#,关于Big Endian 和 Little Endian,以及转换类
Big Endian:最高字节在地址最低位,最低字节在地址最高位,依次排列. Little Endian:最低字节在最低位,最高字节在最高位,反序排列. 当在本地主机上,无需注意机器用的是Big En ...
- Hive variable demo
create table ori_trans (account string, maker string, tdate string) partitioned by (country string); ...
- Hadoop 2.0 中的资源管理框架 - YARN(Yet Another Resource Negotiator)
1. Hadoop 2.0 中的资源管理 http://dongxicheng.org/mapreduce-nextgen/hadoop-1-and-2-resource-manage/ Hadoop ...
- [转]jQuery UI Dialog Modal Popup Yes No Confirm example in ASP.Net
本文转自:http://www.aspsnippets.com/Articles/jQuery-UI-Dialog-Modal-Popup-Yes-No-Confirm-example-in-ASPN ...
- hadoop data 相关开源项目(近期学习计划)
计划学习几个hadoop相关的开源项目: 1.spring hadoop 2.spring batch 3.spring redis 4.spring mongo 相关项目样例:https://git ...
- Android逆向工程初步(一) 15.4.24
最近看了看Android的逆向工程,破解的书,像是<Android Hack‘s Book>之类的,感觉挺有意思的,看了看一些smali的语法,试着自己写了个demo玩玩: 1.工具: 最 ...
- "德意志之歌"的历史
1841年8月, 词作家奥古斯特在当时属于英国的黑尔格兰岛上度假时, 创作了"德意志之歌"的歌词.9月, 这首歌首次出版发行, 曲调则借用了海顿的一首君主颂歌 - "皇帝 ...
- 第8课 goto 和 void 分析
1. 遭人遗弃的goto (1)高手潜规则:禁用goto (2)项目经验:程序质量与goto出现的次数成反比 (3)最后的判决:将goto打入冷宫(1)循环语句的基本工作方式 [实例分析]goto副作 ...
- 【shiro】shiro学习笔记1 - 初识shiro
[TOC] 认证流程 st=>start: Start e=>end: End op1=>operation: 构造SecurityManager环境 op2=>operati ...