2016-01-12 19 views
10

Tôi đang làm việc trên menu bật lên trong thanh tác vụ. Nhưng tôi bị mắc kẹt để hiển thị chính xác dưới đây của actionbar (cut-to-cut). Tôi đang đặt hai snapshot.Làm cách nào để hiển thị menu bật lên chính xác bên dưới thanh tác vụ?

tôi ảnh chụp màn hình vấn đề:

enter image description here

Tôi muốn popup menu chính xác bên dưới của actionbar như sau screenshot

Correction ảnh chụp màn hình:

enter image description here

đoạn mã của tôi:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> 
<item android:id="@+id/action_filter" 
    android:icon="@drawable/ic_filter_white_18dp" 
    android:title="@string/action_filter" 
    app:showAsAction="ifRoom" /> 
<item android:id="@+id/action_label" 
    android:icon="@drawable/ic_add_circle_outline_white_18dp" 
    android:title="@string/action_label" 
    app:showAsAction="ifRoom" /> 
<item android:id="@+id/action_settings" 
    android:title="@string/action_settings" 
    app:showAsAction="never" /> 

+0

Vui lòng không đăng ảnh chụp màn hình hoặc liên kết. Hãy đăng mã của bạn thay vì – Andrew

+0

tôi đặt đoạn mã của tôi vào câu hỏi của mình. – Vasant

Trả lời

4

Đặt chủ đề vào manifest android để

android:theme="@android:style/Theme.Holo.Light" 
9

Theo mã của tôi đây là sotution chính xác bằng cách thay đổi phong cách.

<style name="AppTheme" parent="@android:style/Theme.Holo.Light"> 
    <item name="android:actionBarStyle">@style/MyActionBar</item> 
    <item name="android:actionMenuTextColor">@color/text_white</item> 
    <item name="android:popupMenuStyle">@style/PopupMenu.Example</item> 
    <item name="actionOverflowMenuStyle">@style/OverflowMenu</item> 
</style> 

<style name="PopupMenu.Example" parent="@android:style/Widget.Holo.Light.ListPopupWindow"> 
    <item name="android:popupBackground">#efefef</item> 
</style> 

<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow"> 
    <!-- Required for pre-Lollipop. --> 
    <item name="overlapAnchor">false</item> 

    <!-- Required for Lollipop. --> 
    <item name="android:overlapAnchor">false</item> 
    <item name="android:dropDownVerticalOffset">4.0dip</item> 

</style> 
+0

Thêm bù đắp đã tạo thêm không gian giữa Thanh công cụ và menu trong Android 4.4.4. – Ankit

+2

tôi không sử dụng thanh công cụ ở đây, tôi đã sử dụng thanh tác vụ. – Vasant

+0

cảm ơn bạn đã bao gồm kẹo trước quá – Safeer

1

Bạn có thể đạt được điều này bằng cách sở hữu phong cách overlapAnchor = false

<style name="toolBarStyle" parent="AppTheme"> 
     <item name="popupTheme">@style/toolbarPopup</item> 
    </style> 

    <style name="toolbarPopup" parent="@android:style/Widget.Holo.ListPopupWindow"> <!--ThemeOverlay.AppCompat.Light--> 
     <item name="android:popupBackground">#AF0000</item> 
     <item name="overlapAnchor">false</item> 
     <item name="android:dropDownVerticalOffset">5dp</item> 

    </style> 

và thiết lập phong cách mà trong AppTheme như dưới đây

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
<!-- to avoid overlay of popup window in toolbar --> 
     <item name="actionOverflowMenuStyle">@style/toolBarStyle</item> 
</style> 
+0

"overlapAnchor" yêu cầu mức API tối thiểu 21 –

9

Khi làm việc với AppCompat Theme, mã dưới đây giúp bạn.Được cả Kitkat hoặc Lollipop

Hãy style.xml của bạn như dưới đây

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/black</item> 
    <item name="android:background">@android:color/transparent</item> 
    <item name="actionOverflowMenuStyle">@style/OverflowMenu</item> 
</style> 


<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow"> 
    <item name="android:windowDisablePreview">true</item> 
    <item name="overlapAnchor">false</item> 
    <item name="android:dropDownVerticalOffset">5.0dp</item> 
    <!--<item name="android:popupBackground">#FFF</item>--> 
</style> 
+0

Làm việc một cách hoàn hảo, Cảm ơn người đàn ông –

6

Nếu bạn muốn tiếp tục sử dụng ActionBarAppCompat chủ đề chứ không phải là ToolBar hoặc Holo chủ đề, bạn có thể sử dụng mã này:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="actionOverflowMenuStyle">@style/MyOverflowMenu</item> 
</style> 

<style name="MyOverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow"> 
    <item name="overlapAnchor">false</item> 
    <item name="android:overlapAnchor" tools:ignore="NewApi">false</item> 
    <item name="android:dropDownVerticalOffset">4.0dip</item> 
</style> 

này làm việc cho tôi.

+1

Đã làm việc cho tôi. Cảm ơn. – dazed

+0

Làm việc cho tôi. Thử nghiệm trên Android 4.x, 6.x. Cảm ơn! – hungtdo

Các vấn đề liên quan