In this tutorial we will create an app that consists of Android Custom Action Bar with a custom layout. We assume that you have a basic understanding of the ActionBar component discussed in this tutorial.

在本教程中,我们将创建一个包含具有自定义布局的Android自定义操作栏的应用。 我们假定您对本教程中讨论的ActionBar组件有基本的了解。

Android自定义操作栏 (Android Custom Action Bar)

To customise an ActionBar first we need to configure the Theme in the res/values/styles.xml and set the theme for the respective activity class in the AndroidManifest.xml. Following is the xml layout for that:

要自定义ActionBar,首先我们需要在res/values/styles.xml配置Theme ,并在AndroidManifest.xml为各个活动类设置主题。 以下是该文件的xml布局:

styles.xml

styles.xml

<resources><style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --></style><style name="CustomTheme" parent="Theme.AppCompat.Light"><item name="contentInsetStart">0dp</item><item name="contentInsetEnd">0dp</item></style></resources>

From the above snippet if we use AppTheme style for our activity, it will throw a null pointer exception as it explicitly specifies the NoActionBar theme.

从上面的代码片段开始,如果我们对活动使用AppTheme样式,则它将显式指定NoActionBar主题,因此将引发空指针异常 。

Hence we’ll use the CustomTheme style in this project. The contentInsetStart and contentInsetEnd are the padding values.

因此,我们将在该项目中使用CustomTheme样式。 contentInsetStartcontentInsetEnd是填充值。

Note that we will be using AppCompatActivity since it provides maximum compatibility with pre-3.0 Android versions.

请注意,我们将使用AppCompatActivity因为它与3.0之前的Android版本具有最大的兼容性。

自定义操作栏布局 (Custom Action Bar Layout)

Following is the view layout that will be set to the ActionBar from our MainActivity.

以下是将从MainActivity设置为ActionBar的视图布局。

custom_action_bar_layout.xml

custom_action_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TableRow><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/back"android:layout_gravity="center_vertical"android:background="@android:color/transparent"android:id="@+id/action_bar_back"android:layout_alignParentTop="true"android:layout_centerHorizontal="true" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/app_name"android:gravity="center_horizontal"android:textAppearance="?android:attr/textAppearanceMedium"android:textStyle="bold"android:padding="10dp"android:layout_alignParentTop="true"android:layout_weight="1"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/forward"android:id="@+id/action_bar_forward"android:layout_gravity="center_vertical"android:background="@android:color/transparent"android:layout_alignParentTop="true"android:layout_centerHorizontal="true" /></TableRow></TableLayout>

The view layout consists of two ImageButtons that represent forward and back image buttons and a TextView in the center.

视图布局由两个ImageButton组成,分别代表前向和后向图像按钮,以及一个位于中心的TextView。

Android自定义操作栏项目结构 (Android Custom Action Bar Project Structure)

Android自定义操作条形码 (Android Custom Action Bar Code)

The activity_main.xml is an empty RelativeLayout since our emphasis here is on the ActionBar.
The MainActivity.java is given below.

由于我们这里的重点是ActionBar,所以activity_main.xml是一个空的RelativeLayout。
MainActivity.java在下面给出。

package com.journaldev.customactionbar;import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);getSupportActionBar().setDisplayShowCustomEnabled(true);getSupportActionBar().setCustomView(R.layout.custom_action_bar_layout);View view =getSupportActionBar().getCustomView();ImageButton imageButton= (ImageButton)view.findViewById(R.id.action_bar_back);imageButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {finish();}});ImageButton imageButton2= (ImageButton)view.findViewById(R.id.action_bar_forward);imageButton2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplicationContext(),"Forward Button is clicked",Toast.LENGTH_LONG).show();}});}}

In the above code we’re using support libraries. Hence we’ve used getSupportActionBar() instead of getActionBar().

在上面的代码中,我们正在使用支持库。 因此,我们使用了getSupportActionBar()而不是getActionBar()

To add a custom layout to the ActionBar we’ve called the following two methods on the getSupportActionBar() :

为了向ActionBar添加自定义布局,我们在getSupportActionBar()上调用了以下两个方法:

  • getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);getSupportActionBar()。setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  • getSupportActionBar().setDisplayShowCustomEnabled(true);getSupportActionBar()。setDisplayShowCustomEnabled(true);

setCustomView() is invoked to inflate the ActionBar with a customView as shown above.

如上所示,将调用setCustomView()以使用customView为ActionBar充气。

To set the onClickListeners for the ActionBar buttons we need to get the CustomView first using getCustomView().
In this tutorial we’ve programmed the back button to close the activity using finish(); and the forward button to display a Toast.

要为ActionBar按钮设置onClickListeners,我们需要首先使用getCustomView()获取CustomView。
在本教程中,我们对后退按钮进行了编程,以使用finish();关闭活动finish(); 和前进按钮以显示Toast。

Note : Add the following line in the AndroidManifest.xml inside the application tag.

注意:在AndroidManifest.xml的application标记内添加以下行。

android:theme="@style/CustomTheme"

Here is our android application with custom theme and layout.

这是带有自定义主题和布局的android应用程序。

Note: There is a fixed margin on either sides that can’t be modified. For that we’ll need to replace the ActionBar with a ToolBar. We’ll discuss on that in a later tutorial.

注意:两侧都有固定的边距,无法修改。 为此,我们需要将ActionBar替换为ToolBar。 我们将在以后的教程中对此进行讨论。

This brings an end to android custom action bar tutorial. You can download the final Android CustomActionBar Project from the below link.

这结束了android自定义操作栏教程。 您可以从下面的链接下载最终的Android CustomActionBar项目。

Download Android Custom Action Bar Project下载Android自定义操作栏项目

Reference: Android Doc

参考: Android文档

翻译自: https://www.journaldev.com/9952/android-custom-action-bar-example-tutorial

Android自定义操作栏示例教程相关推荐

  1. Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现

    嘿嘿嘿,关于android滑动的操作,是不是经常都会用到呢. 我肯定也要学习一下啦. https://blog.csdn.net/u013184970/article/details/82882107 ...

  2. Android 自定义操作成功的loading动画

    1.res---->values文件夹下添加文件attrs.xml详情如下: <?xml version="1.0" encoding="utf-8" ...

  3. Android FrameLayout和AbsoluteLayout示例教程

    In this tutorial we'll dive into Android FrameLayout and Android AbsoluteLayout. This is the second ...

  4. 【Android】【手机适配】Android自定义导航栏和全面屏适配方案

    名词说明 状态栏:StatusBar,手机上方显示电量.时间的横条 导航栏:NavigationBar,手机下方显示虚拟按键的横条 标题栏:ActionBar,应用顶部显示标题的横条 全面屏:界面内容 ...

  5. Android SeekBar和RatingBar示例教程

    In this tutorial we'll implement a SeekBar and a RatingBar in our android application. Before we jum ...

  6. android简单的自定义按钮,Android 自定义button简单示例

    >>>>>>>>>>>>>>>>>>> 很多时候android常用的控件不能满足我们的 ...

  7. Android文件操作代码示例,sd卡数据储存

    1. public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle save ...

  8. Android 自定义折线图实现教程

    前言: 各位同学大家好,有段时间没有给大家更新文章了,具体多久我也记不清楚了.最近重新复习了一下原生安卓的知识点,写了一个安卓原生自定义折线图的效果,就想着分享给大家.希望帮助到各位学习和工作,那份废 ...

  9. android自定义进度条_Android中的自定义进度栏

    android自定义进度条 Custom progress bar in android application gives it a personal touch. In this tutorial ...

最新文章

  1. 为数字资产交易设计安全的钱包架构
  2. 2020中国硬科技创新白皮书
  3. thinkphp-查询某一列的值column
  4. 关于“稳定”的相爱相杀:负载测试和压力测试
  5. 将mysql数据库中的图片读出来的_MySQL存入图片+Qt读入读出数据库中的图片
  6. Auto.js微信抢红包脚本
  7. HTTP请求的过程和原理
  8. matlab插值函数的作用,matlab 插值函数
  9. WGS84与GCJ02经纬度坐标转换介绍
  10. nodejs 视频教程《一起学nodejs》
  11. 应用实践 | Apache Doris 在网易互娱的应用实践
  12. 中搜网络与齐齐哈尔达成战略合作 携手共建智慧城市
  13. Redis的使用【Redis】
  14. php引用字体,thinkphp引入字体文件时候被当做模块求解决问题
  15. 《麦肯锡精英的48个工作习惯》书评
  16. 查看本机MAC地址的方法
  17. 转行程序员后,我开始后悔没做这件事
  18. 【图的存储】邻接多重表
  19. 计算机辅助电路与设计试卷,计算机辅助电路设计_习题集(含答案).doc
  20. imx8 qca6595驱动编译

热门文章

  1. 【皇甫】☀PPT里的小玩意
  2. [leetcode]Longest Consecutive Sequence
  3. IE6 的 hover 伪类 bug
  4. using关键字的用法以及作用
  5. [转载] python-pandas创建Series数据类型
  6. [转载] Python中pandas dataframe删除一行或一列:drop函数
  7. vue-resource中文文档
  8. 从后台获取的数据渲染到页面中的dom操作
  9. 使用maven在netbeans下构建wicket项目
  10. DT大数据梦工厂 第55,56讲