2016-01-27 34 views
1

我在我的導航抽屜中使用支持NavigationView來顯示項目菜單。將ActionView設置爲支持NavigationView中的項目不顯示

<android.support.design.widget.NavigationView 
    android:id="@+id/drawer_navigation" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="@drawable/drawable_background" 
    app:headerLayout="@layout/drawer_header" 
    app:itemBackground="@drawable/drawer_item_background" 
    app:itemIconTint="@color/drawer_item" 
    app:itemTextColor="@color/drawer_item" 
    app:menu="@menu/drawer" 
    app:theme="@style/Text.DrawerItem"/> 

我想顯示菜單項旁邊的連接請求計數器。

我試圖設置actionLayout在XML菜單定義這樣

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <group android:checkableBehavior="single"> 
    ... 
    <item 
     android:id="@+id/navigation_requests" 
     android:icon="@drawable/ic_swap_horiz_24dp" 
     android:title="@string/connect_menu_connection_requests" 
     app:actionLayout="@layout/connection_request_counter" 
     app:showAsAction="always"/> 
    ... 
    </group> 
</menu> 

connection_request_counter.xml的ActionView

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:textColor="@color/brand_blue" 
     android:text="5"/> 

的佈局,但在這一點上,我不能看到5號旁該項目。

我試圖設置ActionLayout編程方式,以及:

MenuItemCompat.setActionView(mMenu.findItem(R.id.navigation_requests), R.layout.connection_request_counter); 

但仍沒有運氣。

我可以參考的TextView像

final TextView connectionRequestCounterView 
      = (TextView) MenuItemCompat.getActionView(mMenu.findItem(R.id.navigation_requests)); 

這實際上返回一個TextView實例,但即使我設置文本給它,我不能看到任何佈局。這可能與我正在設計菜單項目的事實有關嗎? (我設置文本顏色,色調和背景的菜單項)

這裏是我的風格:

<style name="Text.DrawerItem"> 
    <item name="android:textColor">@color/text_color_dark_grey</item> 
    <item name="android:textSize">@dimen/text_size_default</item> 
    <item name="android:clickable">false</item> 
    <item name="android:textColorLink">@color/text_color_dark_grey</item> 
    <item name="fontPath">@string/font_aller_light</item> 
    <!--<item name="android:background">@color/background_activity_default</item>--> 
    <!--<item name="background">@color/background_activity_default</item>--> 
</style> 

drawer_item_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!--To override the default grey background for selected item--> 
    <item android:drawable="@android:color/transparent" android:state_checked="true"/> 
    <item android:drawable="@android:color/transparent"/> 
</selector> 

drawer_drawablbe.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item> 
    <shape android:shape="rectangle"> 
     <solid android:color="@color/background_activity_default"/> 
    </shape> 
    </item> 
    <item 
    android:bottom="24dp" 
    android:left="16dp"> 
     <bitmap 
     android:gravity="left|bottom" 
     android:src="@drawable/my_drawer_logo"/> 
    </item> 

</layer-list> 

在哪裏@drawable/my_drawer_logo是一個不同密度的PNG。

任何其他想法爲什麼我不能看到動作視圖?

+0

我試圖重新創建問題,但是我沒有運行代碼的問題。你可以分享'@ style/Text.DrawerItem'嗎? – OYRM

+0

事實上,繼續創建這個問題沒有成功,你可以使用「@ drawable/drawer_item_background」和「@ drawable/drawable_background」嗎? – OYRM

+0

@OYRM非常感謝您的關注。我已經發布了我的樣式和背景可繪畫。但是,我試圖刪除背景drawables並沒有幫助。 –

回答

1

我終於找出是什麼原因導致了這個問題。 對於NavigationView與行動的意見正常工作,您必須使用程序兼容性支持庫版本23.1

所以不是

compile 'com.android.support:appcompat-v7:22.3.0' 

我不得不更新到

compile 'com.android.support:appcompat-v7:23.1.1' 

這使的伎倆和導航抽屜導航視圖中的操作視圖開始正確顯示,完全按照我的意願顯示。


更新到我遇到幾個問題來了,如同ClassNotFoundException新程序兼容性版本顯示啓動的應用程序,這是我固定通過更新所有com.android.support庫最新版本的時候了:

compile 'com.android.support:appcompat-v7:23.1.1' 
compile 'com.android.support:design:23.1.1' 
compile 'com.android.support:recyclerview-v7:23.1.1' 
compile 'com.android.support:support-annotations:23.1.1' 
... 

然後我仍然得到NullPointerException在我的頁眉佈局設置爲NavigationView。如果您在代碼中設置app:headerLayout="@layout/drawer_header"或類似代碼,則在AppCompat版本22中,可以通過findViewById()獲取標題視圖。

程序兼容性版本23,雖然使用RecyclerView所有項目包括標題視圖,因此要獲得參考其意見的方式如下:

mHeaderView = navigationView.getHeaderView(HEADER_INDEX); 

哪裏HEADER_INDEX是最有可能的0,如果你不添加多個標題。

1

我可以很容易地改變櫃檯的文字。但是,我使用的文本視圖是一個LinearLayout內,它有一個機器人:ID以及(這個我試過沒有LinearLayout沒有問題):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/textCounter" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     android:textColor="@color/colorPrimary" 
     android:text="5"/> 
</LinearLayout> 

鑑於這種情況,我能看到,訪問並使用以下方法修改主要活動中的文本視圖:

TextView countText =(TextView)findViewById(R.id.textCounter);

countText.setText(「6」);

當然,我使用一個簡單的方法來計算值,只是將它設置爲值「6」,但我不知道你的計數器結構實際上是什麼樣子,而這些不是主要的關注你的問題。

您查看櫃檯號的能力問題仍然是一個謎。看到我上面的問題,我們也會找出這個元素。

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
} 
+0

我試圖將TextView包裝在FrameLayout中並給它一個ID。該應用程序不會崩潰,但文本仍然不可見。該死的櫃檯。 –

+0

@anoniim我爲Activity添加了我的'onCreate'文本。這與你的相比如何? – OYRM

+1

經過幾個小時後,試圖找出什麼是錯誤的,我明白了!我沒有使用最新版本的支持庫!無論如何,非常感謝所有的努力! –

相關問題