在android中获取ActionBar TextView(标题)

时间:2014-06-21 17:18:50

标签: android android-actionbar

根据标题,我需要检索出现在ActionBar中的活动标题的textview。我可以使用以下方法获取ActionBar视图:

private View getActionBarView() {
    View v = mWindow.getDecorView();
    int resId = getResources().getIdentifier("action_bar_container", "id", "android");
    return v.findViewById(resId);
}

有没有人知道TextView的id,所以我可以在actionbar视图中找到ViewById?

解决: 好吧,我用相同的方法解决了但是改变了id:

private TextView getActionBarTitle() {
    View v = mWindow.getDecorView();
    int resId = getResources().getIdentifier("action_bar_title", "id", "android");
    return v.findViewById(resId);
}

3 个答案:

答案 0 :(得分:6)

所有内置的Android布局都可以在SDK中找到:

.../android-sdk/platforms/android-<API-VERSION>/data/res/layout/

我认为在这种情况下你要找的那个是action_bar_title_item.xml 这是v19的:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical|start"
              android:orientation="vertical"
              android:paddingEnd="8dp">
    <TextView android:id="@+id/action_bar_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:singleLine="true"
              android:ellipsize="end" />
    <TextView android:id="@+id/action_bar_subtitle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="@dimen/action_bar_subtitle_top_margin"
              android:singleLine="true"
              android:ellipsize="end"
              android:visibility="gone" />
</LinearLayout>

布局在API版本之间可以有所不同。一般来说,ID会匹配,但我不能说这对每一个布局都是如此,如果你打算在这种情况下计划使用这样的特定内置资源,那么最好快速查看每个的差异。< / p>

为此,您必须获取源代码的各个副本才能查看每个API版本 这可以通过以下方式完成:

  • 您的IDE:将Android的ADT插件用于您选择的IDE(Eclipse或AndroidStudio)
  • 手动方法:自行调用SDK的版本,该版本位于SDK中:

    ... / Android的SDK /工具/机器人

你显然只需要获取你支持的版本,正如我上面提到的,每个版本都有自己的文件夹,以其单个int版本号命名。例如:

.../android-sdk/platforms/android-19/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-15/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-11/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-10/data/res/layout/???

嗯,哦!没有API-10的布局。如果要在该API版本中使用action_bar_title_item.xml布局,可以包含并使用Sherlock ActionBar,也可以将v11布局复制或修改为:

.../YourApp/res/layout-v10/action_bar_title_item.xml               (Eclipse)
.../YourApp/app/src/main/res/layout-v10/action_bar_title_item.xml  (Android Studio)

你的电话。您只需要自定义布局即可。将Android内置版本复制到您自己的布局文件夹中,并调整到您心中的内容。请注意放置文件的文件夹,以便它们覆盖所需的正确版本。

答案 1 :(得分:2)

2016年,对于其他得到此问题的开发者

在这里答案: https://stackoverflow.com/a/32614598/4548520

使用此工具https://developer.android.com/studio/profile/hierarchy-viewer-walkthru.html 我们可以找到工具栏的任何子项(操作栏)

enter image description here

来自截图:

  • AppCompatTextView(id 0) - title
  • AppCompatTextView(id 2) - 字幕

两者都不包含 action_bar_title 之类的ID

Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
TextView textView = (TextView) toolbar.getChildAt(0);
TextView textView = (TextView) toolbar.getChildAt(2);

答案 2 :(得分:0)

如果您使用的是支持工具栏而不是ActionBar,可以采用以下方式获取标题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;
    }
}