Android之如何优雅的管理ActionBar

article/2025/11/7 9:14:50

转载请标明出处:
http://blog.csdn.net/hai_qing_xu_kong/article/details/50997095
本文出自:【顾林海的博客】


##前言 随着项目越来越大,页面数也相应的增加,每个页面都需要顶部的ActionBar,如果在每个xml文件中去include我们的actionbar布局,工作量也是很大,而且不利于后期维护。今天这篇文章是教大家合理的管理我们的actionbar,代码很简单,主要是一个思路。

注意看actionbar,效果图:

actionbar


##管理ActionBar

为了达到actionbar的统一管理,这里需要获取系统的ActionBar。通过以下方法进行布局的替换:

public abstract void setCustomView(View view, LayoutParams layoutParams);

1、先创建我们的actionbar布局,整体布局很简单,包含左边返回按钮、左边标题、中间标题和右边标题。具体情况以下common_action_bar.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="57dp" ><LinearLayoutandroid:id="@+id/ll_common_back"android:layout_width="30dp"android:layout_height="57dp"android:gravity="center_vertical"android:orientation="horizontal" ><ImageViewandroid:id="@+id/iv_common_back"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:src="@drawable/common_left" /></LinearLayout><TextViewandroid:id="@+id/tv_common_left_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/ll_common_back"android:singleLine="true"android:textColor="@android:color/black"android:textSize="16sp" /><TextViewandroid:id="@+id/tv_common_middle_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:singleLine="true"android:textColor="@android:color/black"android:textSize="16sp" /><TextViewandroid:id="@+id/tv_common_right_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"android:singleLine="true"android:textColor="@android:color/black"android:textSize="16sp" /></RelativeLayout>

效果图如下:

actionbar效果图

2、创建完actionbar布局后,我们新建一个继承Activity的BaseActionBarActivity抽象类,获取系统actionbar,进行布局的替换:

		mActionBar = getActionBar();if (mActionBar != null) {mActionBar.setDisplayShowCustomEnabled(true);mActionBar.setDisplayShowTitleEnabled(false);mActionBar.setDisplayShowHomeEnabled(false);mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.common_bg));initView(mCommonActionBar);ActionBar.LayoutParams params = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);mActionBar.setCustomView(mCommonActionBar, params);

mCommonActionBar就是我们前面定义的actionbar布局View。

3、在BaseActionBarActivity中定义一些方法,用于子类调用,如下:

/*** 设置左边的标题* * @param leftTitle*/protected void setLeftTitle(String leftTitle) {tv_common_left_title.setText(leftTitle);}/*** 设置中间标题* * @param middleTitle*/protected void setMiddleTitle(String middleTitle) {tv_common_middle_title.setText(middleTitle);}/*** 设置右边标题* * @param rightTitle*/protected void setRightTitle(String rightTitle) {tv_common_middle_title.setText(rightTitle);}/*** 主页的Actionbar样式*/protected void setMainAction() {ll_common_back.setVisibility(View.GONE);}/*** 子页面通用样式*/protected void setCommonAction() {iv_common_back.setVisibility(View.VISIBLE);}

4、创建我们的点击事件的回调,在BaseActionBarActivity中定义一个抽象方法:

protected abstract void onClickActionBar(int type);

方法的具体实现由子类实现,type的值有以下几种:

protected final int ACTION_LEFT = 0x01;// 左边标题点击
protected final int ACTION_MIDDLE = 0x02;// 中间标题点击
protected final int ACTION_RIGHT = 0x03;// 右边标题点击

最后注册点击事件,调用相应的方法:

	/*** 事件注册*/private void initEvent() {/*** 返回*/ll_common_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {onBackPressed();}});/*** 右边标题点击*/tv_common_right_title.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {onClickActionBar(ACTION_RIGHT);}});/*** 左边标题点击*/tv_common_left_title.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {onClickActionBar(ACTION_LEFT);}});/*** 中间标题点击*/tv_common_middle_title.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {onClickActionBar(ACTION_MIDDLE);}});}

5、到这里启动程序,会出现背景黑色闪屏,在style文件中定义以下:

<style name="TranslucentTheme" ><item name="android:windowContentOverlay">@null</item>
</style>

别忘了在AndroidManifest.xml中引用:

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/TranslucentTheme" ><activityandroid:name=".MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name="com.example.actionbarbuildproject.SecondActivity"></activity>
</application>

##优雅的使用ActionBar

到这里定义的ActionBar已经创建完毕,接下来我们的子Activity继承我们的BaseActionBarActivity,如下:

package com.example.actionbarbuildproject;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;import com.example.actionbarbuildproject.base.BaseActionBarActivity;public class MainActivity extends BaseActionBarActivity {private Button btn_start;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setMainAction();setMiddleTitle("主页");initViews();initEvent();}private void initViews() {btn_start = (Button) findViewById(R.id.btn_start);}private void initEvent() {btn_start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this,SecondActivity.class));}});}@Overrideprotected void onClickActionBar(int type) {if (type == ACTION_MIDDLE) {Toast.makeText(this, "点击了中间标题", Toast.LENGTH_SHORT).show();}}}

在onCreate方法中通过setMainAction和setMiddleTitle方法实现样式的订制,实现onClickActionBar抽象方法,根据type值获取的我们点击事件,由此ActionBar已经被我们统一管理了,最后使用是不是很简单,当然,这里只是一个思路,actionbar的样式可以根据业务需求来进行订制。

以下是完整的github项目地址,欢迎star,fork。
github项目源码地址:点击【项目源码】


http://chatgpt.dhexx.cn/article/iwC4A1Li.shtml

相关文章

Android Action Bar

1、Action Bar隐藏 如果想整个程序没有Action Bar&#xff0c;把Application的theme设置成NoActionBar即可&#xff0c;如果只是想某个activity没有Action Bar&#xff0c;把Activity的theme设置成NoActionBar即可。 2、Java隐藏显示 actionBar getSupportActionBar(); //获取…

Android 顶部标题栏ActionBar详解

转载自http://www.cnblogs.com/yc-755909659/p/4290784.html 一、ActionBar介绍 在Android 3.0中除了我们重点讲解的Fragment外&#xff0c;Action Bar也是一个非常重要的交互元素&#xff0c;Action Bar取代了传统的tittle bar和menu&#xff0c;在程序运行中一直置于顶部&…

Android中活动条ActionBar的详细使用

图一 图二 图三 图四 图五 ActionBar其提供的功能总结图一使用ActionBar显示选项菜单项 menu_mainxml代码 启用程序图标导航如何添加Action View 图二Activity代码区menu_mainxml代码clockxml 图三使用ActionBar实现Tab导航 创建ActionBar实现Tab导航步骤MainAct…

Action Bar

Action bar是一个标识应用程序和用户位置的窗口功能&#xff0c;并且给用户提供操作和导航模式。在大多数的情况下&#xff0c;当你需要突出展现用户行为或全局导航的activity中使用action bar&#xff0c;因为action bar能够使应用程序给用户提供一致的界面&#xff0c;并且系…

在ActionBar上添加搜索View

在ActionBar上添加搜索View Step1&#xff1a;在Manifests.xml文件中修改属性android:theme&#xff1a;设置深灰色背景的ActionBar android:theme"style/Theme.AppCompat.Light.DarkActionBar"> //设置深灰色背景的ActionBarStep2&#xff1a;在res目录下新建m…

Android 实现ActionBar定制

我们在使用Android手机时&#xff0c;经常发现应用中的ActionBar和我们平时使用的ActionBar相差非常大。简单的说就是&#xff0c;其他应用的ActionBar为什么那么绚丽&#xff0c;自己应用的ActionBar就那么挫呢&#xff1f;最近有时间就仔细研究了一下关于ActionBar的相关问题…

Android_ActionBar

简介 Android3.0之后出现了ActionBar&#xff0c;一种全新的UI设计风格。 Android官网为了使开发者更加了解新UI设计风格&#xff0c;也专门给出了一个板块来介绍design的东西&#xff0c;其中也有ActionBar&#xff08;http://developer.android.com/design/patterns/actionba…

Android之ActionBar详解

自android3.0来Action Bar就取代了OptionsMenu。于是就涉及了API的设置。 1) 设置<uses-sdk android:minSdkVersion"4" android:targetSdkVersion"11" />&#xff0c;表明应用程序可部署于android3.0或之前。也就是说当设备的SdkVersion11时…

Android之ActionBar学习

写于开始前&#xff0c;首先这篇是翻译&#xff0c;源自于自己关于ActionBar的一个问题求教于manoel兄&#xff0c;其csdn博客地址&#xff08;http://blog.csdn.net/manoel?viewmodecontents&#xff09;。后来他发送我一份资料&#xff0c;感觉不错&#xff0c;翻译一下。 关…

ActionBar应用开发讲解

1. 什么是操作栏(ActionBar是android3.0及之后的版本添加的新特性)&#xff1a; 1) ActionBar是一个显示在屏幕顶部的控件&#xff0c;它包括了在左边显示的应用的logo图标和右边操作菜单的可见项。 2) 我们可以选择可见的操作菜单可见项&#xff0c;不可见项会以下拉列表的…

超详细!ActionBar 使用·详解

一、ActionBar介绍 在Android 3.0中除了我们重点讲解的Fragment外&#xff0c;Action Bar也是一个非常重要的交互元素&#xff0c;Action Bar取代了传统的tittle bar和menu&#xff0c;在程序运行中一直置于顶部&#xff0c;对于Android平板设备来说屏幕更大它的标题使用Action…

Android ActionBar完全解析,使用官方推荐的最佳导航栏(下)

转载请注明出处&#xff1a;http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc&#xff0c;我翻译之后又做了些加工&#xff0c;英文好的朋友也可以直接去读原文。 http://developer.android.com/guide/topics/ui/actionbar.html…

Android自定义ActionBar

Android 3.0及以上已经有了ActionBar的API&#xff0c;可以通过引入support package在3.0以下的平台引用这些API&#xff0c;但这儿呢&#xff0c;完全自定义一个ActionBar&#xff0c;不用引入额外jar包&#xff0c;参照的是开源的UI组件GreeenDroid&#xff0c;项目主页&…

什么是ActionBar,详细讲解

更详细讲解 https://blog.csdn.net/b1480521874/article/details/90812777 一、什么是ActionBar 有图有真相&#xff0c;看一下图片就了解的差不多了 对于大多数应用&#xff0c;操作栏可以分割为 4 个不同的功能区域。 <1> 应用图标 ActionBar的图标&#xff0c;可显示…

Android ActionBar的基本用法

本文翻译了这篇文章&#xff1a;Using the Android action bar (ActionBar) - Tutorial 1、ActionBar的简介 ActionBar位于Activity的顶部&#xff0c;可用来显示activity的标题、Icon、Actions和一些用于交互的View。它也可被用于应用的导航。 ActionBar 是在Android 3.0(AP…

Android UI开发详解之ActionBar

在Android3.0之后&#xff0c;Google对UI导航设计上进行了一系列的改革&#xff0c;其中有一个非常好用的新功能就是引入的ActionBar&#xff0c;他用于取代3.0之前的标题栏&#xff0c;并提供更为丰富的导航效果。 一、添加ActionBar 1、不需要导入ActionBar&#xff0c;因为…

超详细!ActionBar使用详解

转自:https://www.cnblogs.com/mjsn/p/6150824.html 一、ActionBar介绍 在Android 3.0中除了我们重点讲解的Fragment外&#xff0c;Action Bar也是一个非常重要的交互元素&#xff0c;Action Bar取代了传统的tittle bar和menu&#xff0c;在程序运行中一直置于顶部&#xff0c…

ActionBar

ActionBar是3.0之后出现的控件&#xff0c;取代menu&#xff0c;显示在标题栏位置。根据我的经验&#xff0c;menu尽量不要使用&#xff0c;很多用户都是小白&#xff0c;根本就不知道menu这个按键有什么用&#xff0c;也不会去点击这个按键。 通过getActionBar()得到对象。 …

什么是ActionBar

原文地址:http://www.2cto.com/kf/201307/229693.html 一、什么是ActionBar 有图有真相&#xff0c;看一下图片就了解的差不多了 对于大多数应用&#xff0c;操作栏可以分割为 4 个不同的功能区域。 1. 应用图标 应用图标是您应用的标志。在应用图标位置摆放您自己的 logo。注意…

Android ActionBar应用实战,高仿微信主界面的设计

转载请注明出处&#xff1a;http://blog.csdn.net/guolin_blog/article/details/26365683 经过前面两篇文章的学习&#xff0c;我想大家对ActionBar都已经有一个相对较为深刻的理解了。唯一欠缺的是&#xff0c;前面我们都只是学习了理论知识而已&#xff0c;虽然知识点已经掌握…