android - 更改自定义工具栏文本

标签 android textview android-toolbar

无法更改工具栏上的文本。我进行了一定程度的搜索,但没有看到任何结果。

我已经尝试了很多东西的组合,但也许会弹出一些东西。 这是我为 Activity 尝试的最后一件事的代码。

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>

最后是我的 list 文件。可能需要一点清洁

 <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>

我有三个 Activity ,Splash,有 2 打 ListView 选项的主屏幕,然后是最终 Activity 屏幕,我希望工具栏根据您点击的内容显示自定义标题。

最佳答案

你忘了设置这个:

 setSupportActionBar(toolbar);

还有这个:

getSupportActionBar().setDisplayShowTitleEnabled(false); 

您的 Activity 是这样的:

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

setSupportActionBar(toolbar);

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

getSupportActionBar().setDisplayShowTitleEnabled(false);

关于android - 更改自定义工具栏文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43643720/

相关文章:

android - 如何在 iOS 中创建滑动页面?就像在 Android 中使用 FragmentStatePagerAdapter

android - 尝试对 LatLng 数组的距离求和

android - 使用 Android Intent 播放多首歌曲

Android 使 TextView 中的超链接可点击

android - 自定义 TextView - 在构造函数之前调用的 setText()

android - 工具栏操作按钮图标颜色

java - 在 Android 中显示图像网格,网格布局中心为空?

android.content.res.Resources$NotFoundException : String resource ID Fatal Exception in Main

java - 使用scrollFlags时自定义工具栏不隐藏

android - 不能将 android.support.v7.widget.Toolbar 与 android.useAndroidX=true 一起使用