2017-04-26 143 views
1

無法讓我的工具欄上的文本更改。我已經做了一個搜索的基調,但我沒有看到任何結果。更改自定義工具欄文本

我已經嘗試了很多東西的組合,但也許會彈出一些東西。 這是我爲我的活動嘗試的最後一件事的代碼。

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> 

我有三個活動,飛濺,其中有2個打列表視圖選項,然後在最後一個活動屏幕,在這裏我想要的工具欄,顯示自定義標題取決於主屏點擊它。

+0

您使用的是supportActionBar()嗎? –

+0

請發佈所有涉及工具欄的代碼。 –

+0

我們還需要您的第三個活動(有工具欄的活動)的代碼。 –

回答

0

同比fogot設置

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

這將是這樣。 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