2014-06-21 40 views
3

根据标题我需要检索表示位于ActionBar中的活动标题的textview。我可以用下面的方法来获取动作条观点:按照标题获取ActionBar TextView(title)android

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

有谁知道的TextView的id,这样我就可以在动作条视图内findViewById?

解决: 好,我解决了用同样的方法,但通过改变ID:

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

回答

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将会匹配,但我不能说每个布局都是如此,如果您打算使用特定的内置资源,则最好快速查看每个差异。

要做到这一点,您必须抓住源代码的单独副本来查看每个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布局,可以包括和使用大侦探动作条或可能复制或修改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版本复制到您自己的布局文件夹中,并调整为您的内容。只要注意将文件放入哪些文件夹,以便它们覆盖所需的正确版本。

+0

Yesssss!谢谢你..要在10分钟内给出答案! – iGio90

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; 
    } 
} 
1

2016年,对其他开发商谁得到了这个问题

这里的答案:使用此工具https://developer.android.com/studio/profile/hierarchy-viewer-walkthru.html 我们能找到的工具栏的任何子女(动作条) https://stackoverflow.com/a/32614598/4548520

enter image description here

来自截图:

  • AppCompatTextView(ID 0) - 标题
  • AppCompatTextView(ID 2) - 字幕

两者不包括ID等action_bar_title

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