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

问题描述

目前它只显示应用程序的名称,我希望它显示一些自定义的内容,并且对于我的应用程序中的每个屏幕都不同.

例如:我的主屏幕可能会在操作栏中显示page1",而应用切换到的另一个活动可能会在该屏幕操作栏中显示page2".

解决方案

更新:最新的 ActionBar (Title) 模式:

仅供参考,

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

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

旧:早期:

=> 正常方式,

您可以通过设置Android:label

来更改每个屏幕的标题(即活动)

 </活动>

=> 自定义 - 标题 - 栏

但是如果你想以自己的方式自定义标题栏,即想要放置图片图标和自定义文本,那么以下代码对我有用:

main.

titlebar.

TitleBar.java

public class TitleBar extends Activity {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);最终布尔值 customTitleSupported =requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);setContentView(R.layout.main);如果(自定义标题支持){getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);}最终 TextView myTitleText = (TextView) findViewById(R.id.myTitle);如果(myTitleText != null){myTitleText.setText("新标题");//用户也可以使用颜色"设置颜色,然后//"颜色值常量"//myTitleText.setBackgroundColor(Color.GREEN);}}}

strings.

strings.values 文件夹下.

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.

解决方案

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. It offers your users a familiar interface across applications that the system gracefully adapts for different screen configurations.
  2. 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.

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.

Read more at: Styling the Action Bar

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

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

Old: Earlier days:

=> Normal way,

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>

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

main.

<?

titlebar.

<?

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.

The strings.values folder.

<?

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

08-30 07:18