如何在操作栏上更改文本 [英] How to change the text on the action bar

查看:56
本文介绍了如何在操作栏上更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

仅供参考,API级别11中引入了

FYI, ActionBar .活动顶部的窗口功能,可以显示活动标题,导航模式和其他交互式项(例如搜索).

Update: Latest ActionBar (Title) pattern:

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.

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

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. 它为您的用户提供了跨应用程序的熟悉界面,系统可以优雅地适应不同的屏幕配置.
  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();

}

设置操作栏样式:

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

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生成样式,则此 样式生成器 工具可以为您提供帮助.

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

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

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

您可以通过设置每个屏幕的 Android:label

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>

=>自定义-标题-条形


但是,如果您想以自己的方式(即 Want to put Image icon and custom-text )自定义标题栏,那么以下代码对我有用:

=> 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:

<?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 文件夹下定义.

strings.xml

The strings.xml file is defined under the values folder.

<?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>

这篇关于如何在操作栏上更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆