更改自定义工具栏文本

12

无法更改我的工具栏上的文本。我已经做了大量搜索,但没有看到结果。

我尝试过很多组合,但也许有些方法会出现。 这是我最后尝试的活动代码。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarCustom);
TextView textView = (TextView)   toolbar.findViewById(R.id.toolbarTextView);
textView.setText("String");

这是我的 XML 代码

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_item_action"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.carson.tryeverything.ItemActionActivity"
android:orientation="vertical"

   >
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbarCustom"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="?attr/colorPrimary"
    >

    <TextView
        android:id="@+id/toolbarTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="Line one"
          android:textAppearance="@android:style/TextAppearance.WindowTitle"
        android:visibility="visible" />
</android.support.v7.widget.Toolbar>


 <FrameLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"

    >


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="6dp"
        android:layout_marginTop="0dp"
        />

  </FrameLayout>

  </LinearLayout>
  android:textAppearance="@android:style/TextAppearance.WindowTitle"
        android:visibility="visible" />
 </android.support.v7.widget.Toolbar>

最后,这是我的清单文件。可能需要稍微整理一下。

 <application
    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/icon2"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar"/>
    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
       >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="no spoilers" />
        </intent-filter>
    </activity>
    <activity android:name=".ItemActionActivity"
        android:theme="@style/AppTheme.NoActionBar"
        />


    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps"/>
</application>

我有三个活动,第一个是“Splash”欢迎屏幕,第二个是主屏幕,其中包含2打的列表视图选项,最后是活动屏幕,在这个屏幕上,我想让工具栏根据您点击的选项显示自定义标题。


你正在使用supportActionBar()吗? - Jonathan Aste
请发布涉及工具栏的所有代码。 - Jonathan Aste
我们还需要您第三个活动的代码(具有工具栏的那个)。 - Leandro Ocampo
1
抱歉,我本以为这是多余的信息,但我添加了我的第三个活动,其中包括我的工具栏代码。我已经尝试了各种涉及 supportActionBar 的代码,但没有任何结果。不过,如果您想让我尝试特定的代码,请告诉我,我会让您知道发生了什么。 - C. Skjerdal
4个回答

14
您忘记设置了这个:
 setSupportActionBar(toolbar);

和这个:

getSupportActionBar().setDisplayShowTitleEnabled(false); 

您的活动就像这样:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarCustom);

setSupportActionBar(toolbar);

TextView textView = (TextView)toolbar.findViewById(R.id.toolbarTextView);
textView.setText("String");

getSupportActionBar().setDisplayShowTitleEnabled(false);

1
这正是我所需要的! - Daniel Beltrami

9

就会是这样。Java代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //here toolbar is your id in xml
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("String"); //string is custom name you want

1
我从来没有理解,为什么没有人直接在XML中编写所有工具栏自定义文本?为什么要在.java文件中编写代码呢?例如:
<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <TextView
                android:id="@+id/tv_toolbar_custom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#fff"
                android:textSize="@dimen/_22ssp"
                android:textStyle="bold"
                android:text="Profile"/>
        </androidx.appcompat.widget.Toolbar>

1
在我的项目中,标题通常取决于传递给活动的数据。 - C. Skjerdal

0

以下是在 Android Kotlin 中更改自定义工具栏文本的方法:

首先,进入 Manifest 文件,在您的活动标签中添加行 android:theme="@style/AppTheme.NoActionBar"

<activity
          android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar">
 </activity>

接下来进入您的Activity并添加以下内容:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        toolbar.title = "My New Title"
}

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