我正在尝试使用 android-support-v7:21 库创建简单的应用程序。
代码 fragment :
MainActivity.java
public class MainActivity extends ActionBarActivity {
Toolbar mActionBarToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_actionbar"
android:background="@null"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:fitsSystemWindows="true" />
</LinearLayout>
但工具栏上显示的不是“我的标题”%应用程序名称%。
好像 setTitle
方法没有效果。
我想显示“我的标题”。
统一更新:
之前,styles.xml
是:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
</style>
所以,我认为没有使用操作栏。
我将 NoActionBar
添加到样式父级:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
</style>
但是问题没有解决。
最佳答案
找到解决办法:
代替:
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);
我用过:
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");
而且它有效。
关于android - 在 android 应用中 Toolbar.setTitle 方法无效 - 应用名称显示为标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486730/