【问题标题】:Toolbar title not showing in Android工具栏标题未在 Android 中显示
【发布时间】:2016-02-07 08:10:37
【问题描述】:

我有这个工具栏:

   <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingtoolbarly"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|snap">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/my_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    android:elevation="4dp"
                    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    />

            </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

我在 Java 中有这个:

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        myToolbar.setTitleTextColor(0xffffffff);
        myToolbar.setBackgroundColor(0xffff0000);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setTitle("My app");

当我运行应用程序时,工具栏显示但不显示标题。如何显示标题?谢谢。

【问题讨论】:

  • getSupportActionBar().setDisplayShowHomeEnabled(true);试试这个以及你以哪种格式提供颜色值?
  • 我试过了,但没用。我的颜色格式是这样的:developer.android.com/reference/android/graphics/…
  • 您是否尝试将标题直接设置为 myToolbar?
  • @VivekMishra 你的意思是布局吗?我试过app:title="My app" ,没用。另外,尝试了android:title,但也没有用。
  • 不喜欢这样 myToolbar.setTitle();

标签: android android-layout android-toolbar


【解决方案1】:

您可以在 CollapsingToolbarLayout 中包含这一行

app:titleEnabled="false"

【讨论】:

    【解决方案2】:

    尝试从应用清单文件中设置标题。试试看

    <activity
            android:name=".MainActivity"
            android:label="@string/title_activity"/>
    

    然后在你的字符串值资源中设置字符串值属性。

    【讨论】:

      【解决方案3】:

      检查下面的链接和它的相关答案。

      Title, etc. not showing in Android Toolbar

      希望对你有帮助!!

      【讨论】:

      • 我检查了那个链接,但没有帮助。
      • 尝试添加这个。 getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      猜你喜欢
      • 2015-03-31
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2015-02-04
      • 2014-03-04
      相关资源
      最近更新 更多