1

我有一些麻煩,當我試圖改變工具欄編程奇怪的問題。改變工具欄時,編程

當我改變從代碼工具欄稱號,並當我運行在真實設備的程序我的工具欄標題是「MyAppName @ 2131558664」,但我只設置「MyAppName」

我也有ViewPager與片段中,我得到的工具欄和工具欄的改變菜單的片段之一(添加搜索查看),當我還啓動程序並打開搜索查看我的查詢也包含此「@ 2131558664」。爲什麼會發生?我該如何解決它? 感謝responces

Toolbar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="?android:actionBarSize" 
    app:theme="@style/MyToolBar" 
    android:layout_centerHorizontal="true" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"> 
    </android.support.v7.widget.Toolbar> 

MyToolBar主題:

<style name="MyToolBar" parent="Theme.AppCompat.Light"> 
     <item name="android:text">@style/TextAppearance.Widget.Event.Toolbar.Title</item> 
     <item name="android:windowActionBarOverlay">true</item> 
     <!-- Support library compatibility --> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="android:textColorPrimary">@color/colorAccent</item> 
     <item name="android:textColorSecondary">@color/colorAccent</item> 

    </style> 

摘錄其中i改變工具欄:

 Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setTitle("MyAppName"); 

回答

0

這是因爲你寫的「機器人:文」的文字,而不是你通過一個主題,而不是文本,因爲你引用一個對象,而不是一個字符串你得到的 - @ 2131558664除。 由於您添加了一段代碼來設置標題,所以實際上會連接兩個字符串。 它有點像寫:

setTitle("my app" + "@2131558664"); 

爲例如,你會看到充滿這些大文件引用,如果你會看你的R檔(如R.id.my_textView):

int attr actionBarTheme 0x7f010060 
int attr actionBarWidgetTheme 0x7f010061 

所以我猜測文字「@ 2131558664」指的是該主題的ID。

幾件事情,你還需要知道:在R檔

1.each ID實際上是即使中有一個字母一個int。

2.如果你會用「的toString()」方法,像TextView的任何部件或任何類似你會得到類似的結果,除非它是一個字符串,那麼你會得到字符串本身。你爲什麼要使用由ID方法實際上由ID連接對象正在創建的對象的查找視圖

3,R檔是不是由用戶和編輯。

它一個很好的例子正從文字和編輯文本

EditText et = (EditText) findViewById(R.id.editText); 
//EditText - this is the object you create 
//R.id.editText - thats the int in the memory 


et.getText(); 
//XXXXX this is thee wrong way to get a string from edit text since 
//its return and editable and if you will pass it to a text view you 
//will get the int with represent the id in the memory. 



et.getText().toString(); 
//this is the right way to get a string from an edit text since it 
//return a string 

你應該嘗試一下,只是爲了好玩和知識= d

+0

感謝您的迴應!問題解決後,我刪除 @ style/TextAppearance.Widget.Event.Toolbar.Title Ololoking

+0

@Ololoking我編輯了我的答案。它會幫助你找出問題所在。 – Roee

0

嘗試移動你的字符串string.xml文件使用getResources().getString(R.string.appTitle); 來讀取string.xml的字符串。

也儘量清理項目,檢查遺漏或錯誤的XML或 資源文件

+0

謝謝你的迴應!這沒有幫助。問題解決了,請檢查我的答案 – Ololoking

0

好吧,我解決我的問題。 這種奇怪的文字dissapear當我刪除

<item name="android:text">@style/TextAppearance.Widget.Event.Toolbar.Title</item>

我不爲什麼它會發生,如果有誰知道請解釋我。

相關問題