如何更改操作栏上的文字

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

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

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

仅供参考, ActionBar在API Level 11中引入。ActionBar是活动顶部的一个窗口function,可显示活动标题 ,导航模式和其他交互项目(如search)。

我记得自定义标题栏,并通过应用程序使其一致。 所以我可以和前几天做一个比较,并列举一些使用ActionBar的优点:

  1. 它为您的用户提供了跨应用程序的熟悉界面,系统可以适应不同的屏幕configuration。
  2. 开发人员不需要编写太多的代码来显示活动标题,图标和导航模式,因为ActionBar已经准备好了最高级别的抽象。

例如:

在这里输入图像描述

在这里输入图像描述

=>正常的方式,

getActionBar().setTitle("Hello world App"); getSupportActionBar().setTitle("Hello world App"); // provide compatibility to all the versions 

=>自定义操作栏,

例如:

 @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(); } 

devise操作栏的样式:

该ActionBar为您提供基本和熟悉的外观,导航模式和其他快速行动来执行。 但这并不意味着它在每个应用程序中都是一样的。 您可以根据您的用户界面和devise要求对其进行定制。 你只需要定义和编写样式和主题。

阅读更多:devise行动栏

如果你想为ActionBar生成样式,那么这个样式生成器工具可以帮助你。

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

旧:早些时候:

=>正常的方式,

您可以通过设置其Android:label来更改每个屏幕的标题(即活动)

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

=>自定义 – 标题栏


但是,如果你想以自己的方式自定义标题栏,即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 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

 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中

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> 

您可以在清单文件中为每个活动定义标签。

一个正常的活动定义如下所示:

 <activity android:name=".ui.myactivity" android:label="@string/Title Text" /> 

标题文本应该由此活动的string资源的标识replace。

如果要dynamic设置标题文本,也可以从代码中设置标题文本。

 setTitle(address.getCity()); 

这条线的标题被设置为我的活动的创build方法中的特定地址的城市。

你可以在你的Activity使用setTitle以编程方式定义你的标题,这个方法可以接受在你的values/strings.xml文件中定义的String或者一个ID。 例:

 public class YourActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { setTitle(R.string.your_title); setContentView(R.layout.main); } } 

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

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

     android:label="@string/TitleWhichYouWantToDisplay" 
  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); } } 

在Activity.onCreate()callback里面,或者在你需要改变标题的地方:

 getSupportActionBar().setTitle("Whatever title"); 

试试这个…

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

它适用于我

 getActionBar().setTitle("edit your text"); 

有点老,但也有同样的问题。 我是这样做的:

strings.xml中

<string name="title_awesome_app">My Awesome App</string>

并确保在AndroidManifest.xml中设置:

 <activity ... android:label="@string/title_awesome_app" > ... </activity> 

这很容易,你不必担心空引用和其他的东西。

更改操作栏名称的最简单方法是转到AndroidManifest.xml并键入此代码。

 <activity android:name=".MainActivity" android:label="Your Label> </activity> 

采用自定义标题栏后,视图中button和checkbox的效果如何? 在我的应用程序中,button和checkbox的样式已更改。 你可以在这里参考我的post

 ActionBar ab = getActionBar(); TextView tv = new TextView(getApplicationContext()); LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, // Width of TextView LayoutParams.WRAP_CONTENT); tv.setLayoutParams(lp); tv.setTextColor(Color.RED); ab.setCustomView(tv); 

欲了解更多信息请点击此链接:

http://android–code.blogspot.in/2015/09/android-how-to-change-actionbar-title_21.html

最简单的方法是调用this.setTitle(“…”),如果你在活动中。 如果你在一个片段中,只需调用getActivity()。setTitle(“…”);

这样可以让你随时更改标题,无需在setContentView(R.layout.activity_test)之前调用它;

更改操作栏名称的最好方法是转到AndroidManifest.xml并键入此代码。

 <activity android:name=".YourActivity" android:label="NameYouWantToDisplay> </activity>