更改自定义工具栏文本 [英] Change Custom Toolbar Text

查看:87
本文介绍了更改自定义工具栏文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法更改我的工具栏上的文本.我已经进行了搜索,但是没有看到结果.

Unable to get the text on my toolbar to change. I have done a tone of searching but I am seeing no results.

我尝试了很多事情组合,但也许会弹出. 这是我为活动尝试的最后一件事的代码.

I have tried so many combinations of things but maybe something will pop out. Here is the code for the last thing I have tried for my 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>

最后这是我的清单文件.大概需要清洁

Lastly here is my Manifest File. Probably needs a little cleaning

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

我有三个活动,飞溅,主屏幕有2打列表视图选项,然后是最后一个活动屏幕,我希望工具栏根据您单击的位置显示自定义标题.

I have three activities, the Splash, the main screen which has 2 dozen listview options and then the final activity screen where I want the toolbar to show a custom title depending on which you click on.

推荐答案

您忘记设置此项:

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

这篇关于更改自定义工具栏文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆