如何更改android操作栏的标题和图标 [英] How do I change the android actionbar title and icon

查看:82
本文介绍了如何更改android操作栏的标题和图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 的 ActionBar 上做一些事情.

I'm trying to do some things on the ActionBar in Android.

我已经在操作栏的右侧添加了新项目.

I've already added new items in the right side of the action bar.

如何更改操作栏的左侧?我想更改图标和文本,我想在其他屏幕的操作栏中添加一个返回按钮"

How can I change the left side of the action bar? I want to change the icon and the text, and I want to add a "Back Button" in the action bar for the other screens

推荐答案

这个很简单实现

如果你想在代码中改变它,调用:

If you want to change it in code, call:

setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);

并将值设置为您喜欢的任何值.

And set the values to whatever you please.

或者,在 Android 清单 XML 文件中:

Or, in the Android manifest XML file:

<activity android:name=".MyActivity" 
       android:icon="@drawable/my_icon" 
       android:label="My new title" />  

要在您的应用中启用后退按钮,请使用:

To enable the back button in your app use:

 getActionBar().setHomeButtonEnabled(true);
 getActionBar().setDisplayHomeAsUpEnabled(true);

代码应该都放在你的 onCreate 中,这样标签/图标的变化对用户来说是透明的,但实际上它可以在 Activity 的生命周期中的任何地方调用.

The code should all be placed in your onCreate so that the label/icon changing is transparent to the user, but in reality it can be called anywhere during the activity's lifecycle.

这篇关于如何更改android操作栏的标题和图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆