本文介绍了actionLayout在导航抽屉中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下设置:
<android.support.design.widget.NavigationView
style="@style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextAppearance="@style/DrawerTextAppearance"
app:menu="@menu/drawer"/>
menu/drawer.xml
menu/drawer.xml
<menu>
<item
android:id="@+id/messages_item"
android:icon="@drawable/ic_notifications_neg"
app:actionLayout="@layout/counter"
android:title="@string/message_center"/>
<item
android:id="@+id/search_item"
android:icon="@drawable/ic_search_neg"
android:title="@string/search"/>
</menu>
layout/counter.xml
layout/counter.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="26dp"
android:layout_height="26dp"
android:text="55"
style="@style/Bubble"/>
样式:
<style name="Bubble" parent="android:Widget.TextView">
<item name="android:gravity">center</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:background">@drawable/bubble</item>
</style>
这将产生以下结果:
尽管设置了样式的重力,气泡仍显示在顶部.
the bubble is shown at the top despite style's gravity setting.
如何将actionLayout放置在菜单项的中间?
How can I position the actionLayout in the middle of a menu item?
推荐答案
我设法解决了这个问题.
I managed to solve the problem by accident.
我不得不将TextView放在容器中:
I had to put the TextView inside a container:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/counterView"
style="@style/Bubble"
android:layout_width="26dp"
android:layout_height="26dp"
android:text="55"/>
</LinearLayout>
现在计数器显示在菜单项的中间!
Now the counter is shown right in the middle of the menu item!
更新
计数器更改代码:
TextView view = (TextView) drawer.getMenu().findItem(R.id.counter_item).getActionView().findViewById(R.id.counterView);
view.setText("" + count);
这篇关于actionLayout在导航抽屉中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!