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

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

有帮助吗?

解决方案

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

供参考, 操作栏 在 API 级别 11 中引入。ActionBar 是 Activity 顶部的一个窗口功能,可以显示 活动标题, 、导航模式和其他交互式项目(例如搜索)。

我清楚地记得自定义标题栏并使其在整个应用程序中保持一致。所以我可以和之前做一个比较,列出使用ActionBar的一些优点:

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

例如:

enter image description here

enter image description here

=>正常方式,

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 和设计要求对其进行自定义。您只需定义和编写样式和主题。

阅读更多内容: 设置操作栏的样式

如果你想为 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, ,那么以下代码对我有用:

主文件

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

标题栏.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>

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

字符串.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" />

其中标题文字应此活动的字符串资源的ID来代替。

您还可以,如果你想动态设置它设置从代码中的标题文字。

setTitle(address.getCity());

这一行的标题被设置为城市在我的活动的onCreate方法的特定的ADRESS

您可以定义编程使用setTitleActivity标题,该方法可以接受一个或Stringvalues/strings.xml文件中定义的ID。例如:

public class YourActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        setTitle(R.string.your_title);
        setContentView(R.layout.main);

    }
}

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

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

    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()回调或在您需要更改标题的另一个地方:

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

它为我的作品

小年纪大一点的,但有同样的问题。我没有这样的:

<强>的strings.xml

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

和确保在您的的AndroidManifest.xml 设置这样的:

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

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

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

改变操作栏名最简单的方法是去的AndroidManifest.xml并输入该代码。

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

您刚刚在onCreate方法添加以下代码。

setTitle("new title");
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

更改操作栏的名称,最好的办法是去AndroidManifest.xml中 并输入该代码。

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

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

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

对于使用的 AndroidX 并在导航架构组件的未来开发者

而不是设置使用上述方案中的一个工具栏冠军,这可以是很痛苦如果您想要动态设置上一回堆栈变化时,你可以设置的占位符作为在导航的标题片段的图表如下所示:

<fragment
    android:id="@+id/some_fragment"
    android:name="package.SomeFragment"
    android:label="Hello {placeholder}"
    tools:layout="@layout/fragment_some">
    <argument
        android:name="placeholder"
        app:argType="string" />
</fragment>

占位符值必须使用FragmentDirections(经由动作方法)来提供。

据然后在标题和节目等Hello World取代(当placeholder = "World")。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top