【问题标题】:Strange issue when changing toolbar programmatically以编程方式更改工具栏时的奇怪问题
【发布时间】:2016-02-12 12:58:08
【问题描述】:

当我尝试以编程方式更改 ToolBar 时遇到了一些麻烦。

当我从代码中更改 ToolBar 标题,当我在真实设备上运行程序时,我的工具栏标题是“MyAppName @2131558664”,但我只设置了“MyAppName”

我也有带有片段的 ViewPager,在其中一个片段中我得到工具栏并更改工具栏菜单(添加 SearchView ),当我也启动程序并打开 SearchView 我的查询也包含这个“@2131558664”。为什么会发生?我该如何解决? 感谢回复

工具栏.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>

我更改的片段 ToolBar:

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

【问题讨论】:

    标签: android android-fragments android-actionbar android-toolbar


    【解决方案1】:

    尝试将您的字符串移动到 string.xml 文件并使用getResources().getString(R.string.appTitle); 从 string.xml 中读取字符串。

    还尝试清理您的项目并检查 xml 或 资源文件

    【讨论】:

    • 感谢您的回复!这没有帮助。问题已解决,请检查我的答案
    【解决方案2】:

    好的,我已经解决了我的问题。 当我删除时,这个奇怪的文字消失了

    &lt;item name="android:text"&gt;@style/TextAppearance.Widget.Event.Toolbar.Title&lt;/item&gt;

    我不知道为什么会发生,如果有人知道请解释一下。

    【讨论】:

      【解决方案3】:

      发生这种情况是因为您编写了“android:text”,而不是文本,而是传递了主题而不是文本,并且因为您引用了一个对象而不是一个字符串,所以您得到了它 - 另外还有 @2131558664。 由于您添加了那段代码来设置标题,因此您实际上连接了两个字符串。 有点像写作:

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

      如果您要查找 R 文件(如在 R.id.my_textView 中)作为参考,您将看到一个大文件,其中包含以下内容:

      int attr actionBarTheme 0x7f010060
      int attr actionBarWidgetTheme 0x7f010061
      

      所以我猜测文本“@2131558664”指的是该主题的 id。

      您还需要知道的几件事:

      1.R文件中的每个id其实都是一个int,即使里面有一个字母。

      2.如果您将在 TextView 或类似的任何小部件上使用“toString()”方法,您将获得类似的结果,除非它是一个字符串,那么您将获得该字符串本身。

      3. 用户无法编辑 R 文件,这就是为什么您必须使用 find view by id 方法,该方法实际上通过 id 将对象连接到您正在创建的对象。

      一个很好的例子是从中获取文本和编辑文本

      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

      【讨论】:

      • 感谢您的回复!我删除 @style/TextAppearance.Widget.Event.Toolbar.Title 后问题解决了
      • @Ololoking 我编辑了我的答案。它将帮助您找出问题所在。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多