目前它只显示应用程序的名称,我希望它显示一些自定义的东西,在我的应用程序中的每个屏幕都是不同的。
例如:我的主屏幕可以在操作栏中显示“page1”,而应用程序切换到的另一个活动可以在该屏幕操作栏中显示“page2”。
目前它只显示应用程序的名称,我希望它显示一些自定义的东西,在我的应用程序中的每个屏幕都是不同的。
例如:我的主屏幕可以在操作栏中显示“page1”,而应用程序切换到的另一个活动可以在该屏幕操作栏中显示“page2”。
当前回答
试着这样做……
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);
...
...
}
这对我很有用
其他回答
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
试着这样做……
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);
...
...
}
这对我很有用
如果你在活动中,最简单的方法是调用this.setTitle("…")。 如果你在一个片段中,只需调用getActivity().setTitle("…");
这样可以让你随时更改标题,不需要在setContentView(r.b ayout.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(通过action方法)提供。
然后在标题中替换它,并像Hello World一样显示(当占位符= "World"时)。
你可以在Activity中使用setTitle以编程方式定义你的标题,这个方法可以接受在values/strings.xml文件中定义的String或ID。例子:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.your_title);
setContentView(R.layout.main);
}
}