How do I change the android actionbar title and ic

2019-01-03 00:53发布

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

Android Action Bar

15条回答
倾城 Initia
2楼-- · 2019-01-03 01:32

The action bar title will, by default, use the label of the current activity, but you can also set it programmatically via ActionBar.setTitle().

To implement the "Back" (more precisely, "Up") button functionality you're talking about, read the "Using the App Icon for Navigation" section of the Action Bar developer guide.

Finally, to change the icon, the guide covers that as well. In short, the action bar will display the image supplied in android:icon in your manifest's application or activity element, if there is one. The typical practice is to create an application icon (in all of the various densities you'll need) named ic_launcher.png, and place it in your drawable-* directories.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-03 01:35

In Android 5.0 material design guidelines discourage the use of icon in actionBar

to enable it add the following code

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);

credit goes to author of this article

查看更多
萌系小妹纸
4楼-- · 2019-01-03 01:36

This is very simple to accomplish

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.

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);

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.

查看更多
forever°为你锁心
5楼-- · 2019-01-03 01:36

You can change the icon in your by adding whatever icon you want to your respective drawable folders, then changing this line in your AndroidManifest.xml file:

android:icon="@drawable/ic_launcher"

to match whatever the name of your icon is in there. Or put your icon as ic_launcher, if they're the same icon. As for what it says, add or change whatever strings match up to that in your res/values/strings.xml file. Then, once again in your AndroidManifest.xml file, change this line:

android:label="@string/app_name"

to whatever the string you have in their. You'll have to do this for the application as a whole, and whichever activities you want, but the lines are the same.

Hope this helps.

查看更多
beautiful°
6楼-- · 2019-01-03 01:39

This work for me:

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
查看更多
▲ chillily
7楼-- · 2019-01-03 01:40

Add the below code inside an onCreate function in your activity.

setTitle("NewName");
查看更多
登录 后发表回答