简体   繁体   English

如何更改android操作栏的标题和图标

[英]How do I change the android actionbar title and icon

I'm trying to do some things on the ActionBar in Android.我正在尝试在 Android 的 ActionBar 上做一些事情。

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:或者,在 Android 清单 XML 文件中:

<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.代码应该全部放在你的onCreate这样标签/图标的变化对用户来说是透明的,但实际上它可以在活动的生命周期中的任何地方被调用。

To make a single icon be usable by all your action bars you can do this in your Android Manifest.要使单个图标可用于所有操作栏,您可以在 Android 清单中执行此操作。

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

    ...

</application>

You just need to add these 3 lines of code.您只需要添加这 3 行代码。 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在 Android 5.0 材料设计指南中不鼓励在 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:您可以通过将任何您想要的图标添加到您各自的可绘制文件夹中来更改您的图标,然后在您的 AndroidManifest.xml 文件中更改这一行:

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.或者将您的图标作为 ic_launcher,如果它们是相同的图标。 As for what it says, add or change whatever strings match up to that in your res/values/strings.xml file.至于它说的是什么,添加或更改与您的 res/values/strings.xml 文件中匹配的任何字符串。 Then, once again in your AndroidManifest.xml file, change this line:然后,再次在您的 AndroidManifest.xml 文件中,更改以下行:

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.为此,您可以通过两种方式实现:XML 或 Java。 See here: How to change the text on the action bar请参阅此处: 如何更改操作栏上的文本

So:所以:

XML: 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() .默认情况下,操作栏标题将使用当前活动的标签,但您也可以通过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.简而言之,操作栏将在清单的applicationactivity元素中显示android:icon中提供的图像(如果有的话)。 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.典型的做法是创建一个名为ic_launcher.png的应用程序图标(具有您需要的所有不同密度),并将其放置在您的drawable-*目录中。

I got non-static method setTitle(CharSequence) cannot be referenced from a static context error because I used setTitle() in static PlaceholderFragment class.我得到了non-static method setTitle(CharSequence) cannot be referenced from a static context错误中non-static method setTitle(CharSequence) cannot be referenced from a static context因为我在静态 PlaceholderFragment 类中使用了setTitle() I solved it by using getActivity().getActionBar().setTitle("new title");我通过使用getActivity().getActionBar().setTitle("new title");解决了它getActivity().getActionBar().setTitle("new title");

Go to AndroidManifest.xml file.转到AndroidManifest.xml文件。 Find the <application> tag There you can see a attribute找到<application>标签在那里你可以看到一个属性

android:label="@string/app_name"

Now go to res > values > strings.xml现在转到res > values > strings.xml

Change the改变

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

to

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

Example例子

AndroidManifest.xml 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字符串.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")
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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