Android标题不会显示在工具栏中。

35

我有一个xml文件,用于在许多带有片段的活动中使用,但我的问题是我无法在工具栏中显示我想要的文本。我这样使用xml是因为我有一个导航抽屉,并且需要处理一些事情,所以我不得不这样做。

我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StatusActivity"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/ToolBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="@dimen/abc_action_bar_default_height_material" />

</RelativeLayout>

我的其中一个活动:

public class GroupChatActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
         getSupportActionBar().setDisplayShowHomeEnabled(true);

         ActionBar actionBar = getSupportActionBar();
         actionBar.setTitle("Groups history");

        Aniways.init(this);

        if(savedInstanceState == null)
        {
            FragmentManager manager = getSupportFragmentManager();

            Fragment fragment = GroupChatFragment.newInstance(getIntent().getIntExtra("uid", 0));
            manager.beginTransaction().add(R.id.frame_container, fragment).commit();
        }
    }
}

你可以看到,我试图将标题设置为操作栏,但它无法正常工作。


3
尝试调用 getSupportActionBar().setDisplayShowTitleEnabled(true) 然后调用 toolbar.setTitle(title) - Nikola Despotoski
1
@NikolaDespotoski 试过了,不起作用。 - Alex Bitek
尝试调用以下 Kotlin 代码以设置支持操作栏标题:supportActionBar?.title = Html.fromHtml("<font color="black">工具栏标题</font>") - Anshul1507
10个回答

50
getSupportActionBar().setDisplayShowTitleEnabled(true);

1
对我来说,在Android 5.1.1上,即使只使用getSupportActionBar().setTitle,工作也正常,即在工具栏内设置标题,但是如果我尝试直接从工具栏引用设置标题,则不起作用,只能通过getSupportActionBar()设置。 - Alex Bitek

23

设置,

app:title="@string/my_title"

在android.support.v7.widget.Toolbar的声明中,硬编码了工具栏中的标题。

要通过程序设置标题,请使用以下代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setTitle("my title");
setSupportActionBar(toolbar);

在你的活动类中。


3
为什么 android:title 没有抛出任何错误信息?! - Daniel Wilson

8

试试这个方法...这个方法对我有用..!!希望它能帮助到某些人..!!

<android.support.v7.widget.Toolbar  
 xmlns:app="http://schemas.android.com/apk/res-auto"  
 android:id="@+id/my_awesome_toolbar"  
 android:layout_width="match_parent"  
 android:layout_height="wrap_content"  
 android:background="?attr/colorPrimary"  
 android:minHeight="?attr/actionBarSize"  
 app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >  

<TextView  
   android:id="@+id/toolbar_title"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:layout_gravity="center"  
   android:singleLine="true"  
   android:text="Toolbar Title"  
   android:textColor="@android:color/white"  
   android:textSize="18sp"  
   android:textStyle="bold" />  

</android.support.v7.widget.Toolbar>  

编辑

您也可以使用这个。

setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
      getSupportActionBar().setTitle("Toolbar title");

通过这样做,似乎getSupportActionBar()的功能被缩短了。 - Pievis
是的,但是getSupportActionBar()对我不起作用,对于一些更糟糕的情况,我使用这个... - Swaminathan V

3
我花了一整天的时间寻找问题的原因。无论是supportActionBar?.title = "SomeTitle"还是supportActionBar?.setDisplayShowTitleEnabled(true)都没有起作用,而来自这里答案的自定义工具栏的Hack方法看起来也不太好。

我的活动使用CollapsingToolbarLayout但没有显示任何标题,也没有从清单中获取,也没有动态地设置一个标题。但是样例ScrollingActivity(新建-活动-...)显示了标题。

最后我设置了一个示例项目,复制了MyActivityScrollingActivity并查看了差异。

AppBarLayoutCollapsingToolbarLayoutlayout_height必须设置为match_parent或固定大小。就是这样。请参见下面的工作代码。

<com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay"
            >

        <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@id/toolbar"
                app:contentScrim="?attr/colorPrimary"
                >

            <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:layout_collapseMode="pin"
                    />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

你可以通过在CollapsingToolbarLayout上设置app:titleEnabled="false"来实现相同的效果。 - Milan

1

我做了一个自定义的操作栏。

使用以下代码布局iguepardos_action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blanco"
android:minHeight="?attr/actionBarSize">

    <TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:singleLine="true"
    android:text="Toolbar Title"
    android:textColor="@color/naranja"
    android:textSize="18sp" />

</android.support.v7.widget.Toolbar>

在我的类中扩展了 AppCompatActivity,我有以下代码:
protected void onCreate(Bundle savedInstanceState) {
.... 
....

getSupportActionBar().setDisplayShowCustomEnabled(true); // is for specify use custom bar 
getSupportActionBar().setCustomView(R.layout.iguepardos_action_bar);  // my custom layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Button for come back

View mCustomView = getSupportActionBar().getCustomView(); // Set the view
TextView TitleToolBar = (TextView) mCustomView.findViewById(R.id.toolbar_title); // find title control
TitleToolBar.setText("The Title Show"); // Set the Title

}

1
尝试使用toolbar.setTitle('Groups history');

1
“Goals History” 你应该使用双引号传递参数。 - Muhammad Usman

1

getActionBar().setTitle("群组历史");

如果您使用的是AppCompat库,则可以这样写:

getSupportActionBar().setTitle("群组历史");


0

给定的以下代码在我的电脑上可行:

 Toolbar myToolBar = findViewById(R.id.my_toolbar);
 setSupportActionBar(myToolBar);
 getSupportActionBar().setTitle(getString(R.string.toolbar_title_note));

首先我尝试了 getSupportActionBar().setDisplayShowTitleEnabled(true); 和没有它两种情况都可以。

这些是我的项目的 gradle 设置:

minSdkVersion 16
targetSdkVersion 30
compileSdkVersion 30
buildToolsVersion "30.0.3"

0
如果您的标题颜色是白色,而工具栏也是白色,那么您将无法区分它。因此,请尝试更改工具栏颜色。

0

我实际上需要获取toolbar_title来将文本设置到每个不同的活动中:

    toolbar = findViewById(R.id.toolbar);
    toolbarTitle = findViewById(R.id.toolbar_title);   //<----- here
    toolbarTitle.setText(getString(R.string.my_activity_toolbar_title));
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接