简体   繁体   中英

How do I change the android actionbar title and icon

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

安卓操作栏

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.

To make a single icon be usable by all your action bars you can do this in your Android Manifest.

<application
    android:logo="@drawable/Image">

    ...

</application>

You just need to add these 3 lines of code. Replace the icon with your own icon. If you want to generate icons use this

getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);

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

如果您想更改操作栏标题,只需在您的 Activity 的 onCreate() 中提供以下 1 行代码

getActionBar().setTitle("Test");

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.

ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);

For that, you can do it in 2 ways: XML or Java. See here: How to change the text on the action bar

So:

XML:

<activity android:name=".Hello_World"
              android:label="This is the Hello World Application">
</activity>

Java:

public class TitleBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

       setContentView(R.layout.main);


       if ( customTitleSupported ) {
           getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
           }

       final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
       if ( myTitleText != null ) {
           myTitleText.setText("NEW TITLE");

           // user can also set color using "Color" and then "Color value constant"
          // myTitleText.setBackgroundColor(Color.GREEN);
       }
 }
}

For set Title :

getActionBar().setTitle("Title");

For set Icon :

getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);

我在onNavigationItemSelected使用了以下调用:

HomeActivity.this.setTitle(item.getTitle());

在您的活动的 onCreate 函数中添加以下代码。

setTitle("NewName");

转到您要更改的特定活动的清单 操作栏标题名称并写入 android:label="Title name"

This work for me:

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);

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.

I got non-static method setTitle(CharSequence) cannot be referenced from a static context error because I used setTitle() in static PlaceholderFragment class. I solved it by using getActivity().getActionBar().setTitle("new title");

Go to AndroidManifest.xml file. Find the <application> tag There you can see a attribute

android:label="@string/app_name"

Now go to res > values > strings.xml

Change the

<string name="app_name">MainActivity</string> 

to

<string name="app_name">Your Desired Name</string>

Example

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SubmitForm">

        </activity>
    </application>

strings.xml

<resources>
    <string name="app_name">Your Desired Name</string>
    <string name="action_settings">Settings</string>
</resources>

You can also do as follow :

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        setSupportActionBar(toolbar)
        setTitle("Activity 2")
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM