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>

我不为什么它会发生,如果有谁知道请解释我。

相关问题