目前,它仅显示应用程序的名称,而我希望它显示自定义名称,并且对于我的应用程序中的每个屏幕都不同。

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

最佳答案

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

仅供引用,在API级别11中引入了ActionBar。ActionBar是Activity顶部的窗口功能,可以显示 Activity 标题,导航模式和其他交互式项(例如搜索)。

我确实记得自定义标题栏并使其在应用程序中保持一致。因此,我可以与早期进行比较,并列出使用ActionBar的一些优点:

  • 它为您的用户提供了跨应用程序的熟悉界面,系统可以优雅地适应不同的屏幕配置。
  • 开发人员不需要编写太多代码来显示 Activity 标题,图标和导航模式,因为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();
    
    }
    

    设置操作栏样式:

    ActionBar为您提供基本和熟悉的外观,导航模式以及要执行的其他快速操作。但这并不意味着在每个应用程序中看起来都一样。您可以根据您的UI和设计要求自定义它。您只需要定义和编写样式和主题即可。

    阅读更多信息:Styling the Action Bar

    而且,如果您想为ActionBar生成样式,则此Style Generator工具可以为您提供帮助。

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

    旧:较早的日子:

    =>正常方式,

    您可以通过设置每个屏幕的 Android:label 来更改每个屏幕的标题(即“ Activity ”)
       <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>
    

    07-24 09:49
    查看更多