如何更改操作栏上的文本

doo*_*her 227 android android-actionbar

目前它只显示应用程序的名称,我希望它显示自定义的内容,并在我的应用程序中为每个屏幕显示不同的内容.

例如:我的主屏幕可以在操作栏中显示"page1",而应用切换到的另一个活动可以在该屏幕操作栏中显示"page2".

Par*_*ani 348

更新:最新ActionBar(标题)模式:

FYI, ActionBar was introduced in API Level 11. ActionBar is a window feature at the top of the Activity that may display the activity title, navigation modes, and other interactive items like search.

I exactly remember about customizing title bar and making it consistent through the application. So I can make a comparison with the earlier days and can list some of the advantages of using ActionBar:

  1. It offers your users a familiar interface across applications that the system gracefully adapts for different screen configurations.
  2. Developers don't need to write much code for displaying the Activity Title, icons and navigation modes because ActionBar is already ready with top level abstraction.

For example:

在此输入图像描述

在此输入图像描述

=> Normal way,

getActionBar().setTitle("Hello world App");   
getSupportActionBar().setTitle("Hello world App");  // provide compatibility to all the versions
Run Code Online (Sandbox Code Playgroud)

=> Customizing Action Bar,

For example:

@Override
public void setActionBar(String heading) {
    // TODO Auto-generated method stub

    com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
    actionBar.setTitle(heading);
    actionBar.show();

}
Run Code Online (Sandbox Code Playgroud)

Styling the Action Bar:

The ActionBar provides you with basic and familiar looks, navigation modes and other quick actions to perform. But that doesn't mean it looks the same in every app. You can customize it as per your UI and design requirements. You just have to define and write styles and themes.

Read more at: Styling the Action Bar

And if you want to generate styles for ActionBar then this Style Generator tool can help you out.

=================================================================================

Old: Earlier days:

=> Normal way,

you can Change the Title of each screen (i.e. Activity) by setting their Android:label

   <activity android:name=".Hello_World"
                  android:label="This is the Hello World Application">
   </activity>
Run Code Online (Sandbox Code Playgroud)

=> Custom - Title - bar


But if you want to Customize title-bar in your own way, i.e. Want to put Image icon and custom-text, then the following code works for me:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="400dp" 
  android:layout_height="fill_parent"
  android:orientation="horizontal">

<ImageView android:id="@+id/ImageView01" 
            android:layout_width="57dp" 
            android:layout_height="wrap_content"
            android:background="@drawable/icon1"/>

<TextView 

  android:id="@+id/myTitle" 
  android:text="This is my new title" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:textColor="@color/titletextcolor"
   />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

TitleBar.java

public class TitleBar extends Activity {

    @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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

strings.xml

strings.xml文件在该values文件夹下定义.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Set_Text_TitleBar!</string>
    <string name="app_name">Set_Text_TitleBar</string>
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

  • 按照此示例在android中为活动设置自定义标题或在整个应用程序中设置全局:http://labs.makemachine.net/2010/03/custom-android-window-title/ (2认同)
  • @quinestor是的,如果你愿意,你可以使用"wrap_content"或"fill_parent",这只是一个例子. (2认同)

Jan*_*usz 105

You can define the label for each activity in your manifest file.

A normal definition of a activity looks like this:

<activity
     android:name=".ui.myactivity"
     android:label="@string/Title Text" />
Run Code Online (Sandbox Code Playgroud)

Where title text should be replaced by the id of a string resource for this activity.

You can also set the title text from code if you want to set it dynamically.

setTitle(address.getCity());
Run Code Online (Sandbox Code Playgroud)

使用此行,标题将设置为我的活动的oncreate方法中特定地址的城市.


don*_*ner 55

您可以使用setTitle您的Activity方法以编程方式定义标题,此方法可以接受文件中String定义的a 或ID values/strings.xml.例:

public class YourActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        setTitle(R.string.your_title);
        setContentView(R.layout.main);

    }
}
Run Code Online (Sandbox Code Playgroud)


小智 19

我们可以通过以下两种方式之一更改ActionBar标题:

  1. 在清单中:在清单文件中,设置每个活动的标签.

    android:label="@string/TitleWhichYouWantToDisplay"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在代码中:在代码中,使用String或String的id作为参数调用setTitle()方法.

    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            setTitle(R.string.TitleWhichYouWantToDisplay);
            // OR You can also use the line below
            // setTitle("MyTitle")
            setContentView(R.layout.activity_main);
    
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 这就是答案. (2认同)

And*_*rey 13

在Activity.onCreate()回调内或在您需要更改标题的另一个地方:

getSupportActionBar().setTitle("Whatever title");
Run Code Online (Sandbox Code Playgroud)


non*_*ckh 6

试试这个......

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
    this.setTitle(String.format(your_format_string, your_personal_text_to_display));
    setContentView(R.layout.your_layout);
       ...
       ...
}
Run Code Online (Sandbox Code Playgroud)

这个对我有用


Mat*_*elo 5

科特林

您可以通过这种方式在 Kotlin 中以编程方式执行此操作。如果“supportActionBar”为空,您只需忽略它

    supportActionBar?.setTitle(R.string.my_text)
    supportActionBar?.title = "My text"
Run Code Online (Sandbox Code Playgroud)

或者这样。如果“supportActionBar”为空,它会抛出异常。

    supportActionBar!!.setTitle(R.string.my_text)
    supportActionBar!!.title = "My text"
Run Code Online (Sandbox Code Playgroud)