【问题标题】:Getting ActionBar Title TextView with AppCompat v7 r21使用 AppCompat v7 r21 获取 ActionBar 标题 TextView
【发布时间】:2014-10-19 21:26:28
【问题描述】:

我有一个库需要使用 TextView 的颜色作为 ActionBar 标题。在 AppCompat v7 r21 之前,我可以 findViewById() 直接从视图中获取颜色。但是,由于某种原因,现在这不起作用。视图始终为空。我编写了解析整个视图层次结构并打印出所有TextViews 的ID、类型和值的代码。标题视图没有ID,我觉得很奇怪。

我注意到的一件事是,当我尝试获取 ActionBar 时,返回的是一个工具栏(即使我没有在我的应用程序中使用工具栏)。因此,我迭代了Toolbar 的子视图,每当找到TextView 时,我都会将其文本值与toolbar.getTitle() 进行比较,以确保这是我正在寻找的TextView。不理想,我不确定它是否适用于所有情况。

有谁知道什么是最安全的解决方案?

【问题讨论】:

  • 您找到解决方案了吗?
  • 不,我只是坚持通过遍历工具栏的子项来查找标题 TextView 的代码。
  • 在这里看看我的遮阳篷:stackoverflow.com/a/26888120/2742962
  • @BugsBunnyBr 这很好,但如果他们再次更改会员的 ID 或名称,它会中断:(

标签: android android-support-library android-appcompat


【解决方案1】:

通常在我使用工具栏的情况下,如果我对标题进行自定义操作,我将手动扩展标题视图,然后在 XML 中设置其属性。 Toolbar 的重点是防止此类事情发生,因此您可以更好地控制工具栏的外观

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="@dimen/standard_vertical_increment"
        android:layout_width="match_parent"
        android:minHeight="@dimen/standard_vertical_increment"
        android:background="@drawable/actionbar_background">


        <TextView
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/Red" />

    </android.support.v7.widget.Toolbar>

然后在代码中,你会做这样的事情:

Toolbar toolbar = findViewById(R.id.my_awesome_toolbar);
//Get rid of the title drawn by the toolbar automatically
toolbar.setTitle("");
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setTextColor(Color.BLUE);

我知道这是一篇较旧的帖子,但这是我找到的在工具栏中允许自定义 textc 颜色和字体的最佳方法

【讨论】:

  • 谢谢。这样,我可以从应用程序中的任何活动更改颜色工具栏。
  • 如何在滚动时移动它,使用新的设计库和 CollapsingToolbarLayout ?我需要同时使用标题和副标题,但遗憾的是使用新设计库时似乎没有出现副标题
【解决方案2】:

无需创建自己的 TextView,只需循环工具栏子项即可获取内置标题。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// loop through all toolbar children right after setting support 
// action bar because the text view has no id assigned

// also make sure that the activity has some title here
// because calling setText() with an empty string actually
// removes the text view from the toolbar

TextView toolbarTitle = null;
for (int i = 0; i < toolbar.getChildCount(); ++i) {
    View child = toolbar.getChildAt(i);

    // assuming that the title is the first instance of TextView
    // you can also check if the title string matches
    if (child instanceof TextView) {
        toolbarTitle = (TextView)child;
        break;
    }
}

【讨论】:

    【解决方案3】:

    你可以这样做。

    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    TextView title=(TextView )toolbar.getChildAt(0);
    

    这对我有用。

    【讨论】:

    • 我如何设置 getChildAt(0)?例如,我想加载一封电子邮件,如 todo@so.com,上面的代码我该怎么做?
    【解决方案4】:

    编辑(2018 年 9 月):不要使用它,因为 Android P 在 android SDK 上的反射可能会引发异常!

    我使用反射得到它。

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Title"); //important step to set it otherwise getting null
    TextView title = null;
    try{
            Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
            f.setAccessible(true);
            title = (TextView) f.get(toolbar);
            title.setText("I have title TextView");
    } catch(Exception e){
            e.printStackTrace();
    }
    

    请注意,如果我的工具栏设置为支持 ActionBar...

    setSupportActionBar(toolbar); // with this set text view behave differently 
                                  //and text may not be visible...
    

    【讨论】:

      【解决方案5】:

      这是获取标题的未来证明方法,以防 Android 更改图标和文本的顺序。

      //If you just want to set the text
      ActionBar actBar = getSupportActionBar();
      if(actBar != null) {
          actBar.setTitle(R.string.your_ab_title);
      }
      
      //If you want to customize more than the text    
      Toolbar ab = findViewById(R.id.action_bar);
      if(ab != null){
          for (int i= 0; i < ab.getChildCount(); i++){
              View child = ab.getChildAt(i);
              if(child instanceof TextView) {
                  //You now have the title textView. Do something with it
              }
           }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-14
        • 1970-01-01
        • 2014-02-11
        • 1970-01-01
        • 1970-01-01
        • 2015-03-14
        • 2015-08-04
        • 2014-12-18
        相关资源
        最近更新 更多