2014-06-21 37 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);