Android 如何更改操作栏上的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3438276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the text on the action bar
提问by dootcher
Currently it just displays the name of the application and I want it to display something custom and be different for each screen in my app.
目前它只显示应用程序的名称,我希望它显示一些自定义的东西,并且对于我的应用程序中的每个屏幕都是不同的。
For example: my home screen could say 'page1' in the action bar while another activity that the app switches to could have 'page2' in that screens action bar.
例如:我的主屏幕可能会在操作栏中显示“page1”,而应用程序切换到的另一个活动可能会在该屏幕操作栏中显示“page2”。
回答by Paresh Mayani
Update: Latest ActionBar (Title) pattern:
更新:最新的 ActionBar(标题)模式:
FYI, ActionBarwas 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.
仅供参考,ActionBar是在 API 级别 11 中引入的。ActionBar 是 Activity 顶部的窗口功能,可以显示Activity 标题、导航模式和其他交互项,例如搜索。
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:
我完全记得自定义标题栏并通过应用程序使其保持一致。所以我可以与早期进行比较,并可以列出使用 ActionBar 的一些优点:
- It offers your users a familiar interface across applications that the system gracefully adapts for different screen configurations.
- 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.
- 它为您的用户提供了一个熟悉的跨应用程序界面,系统可以优雅地适应不同的屏幕配置。
- 开发人员不需要编写大量代码来显示活动标题、图标和导航模式,因为 ActionBar 已经准备好使用顶级抽象。
For example:
例如:
=> Normal way,
=> 正常方式,
getActionBar().setTitle("Hello world App");
getSupportActionBar().setTitle("Hello world App"); // provide compatibility to all the versions
=> 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();
}
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.
ActionBar 为您提供基本和熟悉的外观、导航模式和其他要执行的快速操作。但这并不意味着它在每个应用程序中看起来都一样。您可以根据您的 UI 和设计要求对其进行自定义。您只需要定义和编写样式和主题。
Read more at: Styling the Action Bar
阅读更多信息:设置操作栏样式
And if you want to generate styles for ActionBar then this Style Generatortool can help you out.
如果你想为 ActionBar 生成样式,那么这个样式生成器工具可以帮助你。
=================================================================================
================================================== ================================
Old: Earlier days:
旧: 早期:
=> Normal way,
=> 正常方式,
you can Change the Title of each screen (i.e. Activity) by setting their Android:label
您可以通过设置它们来更改每个屏幕(即活动)的标题 Android:label
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
=> 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:
但是,如果您想以自己的方式自定义标题栏,即Want to put Image icon and custom-text
,那么以下代码对我有用:
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"/>
titlebar.xml
标题栏.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>
TitleBar.java
标题栏.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);
}
}
}
strings.xml
字符串.xml
The strings.xml file is defined under the values
folder.
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>
回答by Janusz
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" />
Where title text should be replaced by the id of a string resource for this activity.
其中标题文本应替换为此活动的字符串资源的 ID。
You can also set the title text from code if you want to set it dynamically.
如果你想动态设置它,你也可以从代码中设置标题文本。
setTitle(address.getCity());
with this line the title is set to the city of a specific adress in the oncreate method of my activity.
使用这一行,标题设置为我活动的 oncreate 方法中特定地址的城市。
回答by donturner
You can define your title programatically using setTitle
within your Activity
, this method can accept either a String
or an ID defined in your values/strings.xml
file. Example:
您可以通过编程定义标题setTitle
您内Activity
,这种方法可以接受一个String
在你定义或IDvalues/strings.xml
文件。例子:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.your_title);
setContentView(R.layout.main);
}
}
回答by Radheshyam Singh
We can change the ActionBar title in one of two ways:
我们可以通过以下两种方式之一更改 ActionBar 标题:
In the Manifest: in the manifest file, set the label of each Activity.
android:label="@string/TitleWhichYouWantToDisplay"
In code: in code, call the setTitle() method with a String or the id of String as the argument.
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); } }
在Manifest中:在manifest文件中,设置每个Activity的标签。
android:label="@string/TitleWhichYouWantToDisplay"
在代码中:在代码中,以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); } }
回答by Andrey
Inside Activity.onCreate() callback or in the another place where you need to change title:
在 Activity.onCreate() 回调中或在另一个需要更改标题的地方:
getSupportActionBar().setTitle("Whatever title");
回答by nonickh
try do this...
尝试这样做...
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);
...
...
}
it works for me
这个对我有用
回答by Javatar
Little bit older but had the same problem. I did it like this:
有点老了,但有同样的问题。我是这样做的:
strings.xml
字符串.xml
<string name="title_awesome_app">My Awesome App</string>
<string name="title_awesome_app">My Awesome App</string>
and make sure you set this in your AndroidManifest.xml:
并确保在AndroidManifest.xml 中设置:
<activity
...
android:label="@string/title_awesome_app" >
...
</activity>
it's easy and you don't have to worry about null-references and other stuff.
这很容易,您不必担心空引用和其他东西。
回答by Mallikarjun Dodmani
The Easiest way to change the action bar name is to go to the AndroidManifest.xml and type this code.
更改操作栏名称的最简单方法是转到 AndroidManifest.xml 并键入此代码。
<activity android:name=".MainActivity"
android:label="Your Label> </activity>
回答by Habibur Rahman Ovie
getActionBar().setTitle("edit your text");
回答by NEC
You just add this code in the onCreate
method.
您只需在onCreate
方法中添加此代码即可。
setTitle("new title");