在Android中更改标题栏文本 [英] Change title bar text in Android

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

问题描述

如何更改标题栏的文字?截至目前它只是显示的程序想要它显示的东西我选择的,并为每个页面/活动在我的应用程序,即我的主页上可以说,不同的称号和IM第1页中,而另一个活动的标题栏中的应用程序切换为能有2页在该页面的标题栏。

How do I change the text of the title bar? as of now it just displays the title of the program and im wanting it to display something of my choosing and be different for each page/activity in my app i.e. my home page could say page1 in the title bar while another activity that the app switches to could have page2 in that page's title bar.

推荐答案

仅供参考,动作条被引入API级别11的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.

我完全记得有关自定义标题栏,使通过应用程序保持一致。所以,我可以与早期的天比较,可以列出一些使用动作条的优点:

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. 在开发者不需要写太多$ C $下显示的活动名称,图标和导航模式,因为动作条已经准备好与最高级别的抽象。

例如:

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

}

风格化的操作栏:

在动作条提供了基本的和熟悉的外观,导航模式等快速动作来执行。但是,这并不意味着它会在每一个应用程序是相同的。您可以自定义它根据你的用户界面和设计要求。你只需要定义和书写风格和主题。

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.

更多详情:风格化的操作栏

如果你想生成样式动作条则此 风格发电机 工具可以帮您解决。

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

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

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

您可以通过设置改变每个屏幕的标题(即活动)的 安卓标签

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>

=>自定义 - 标题 - 酒吧


但是,如果你想自定义标题栏以自己的方式,即 想要把图片图标,自定义文本 ,然后下面的code对我的作品:

=> 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文件,在 值定义 文件夹。

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>

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

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