2016-01-12 10 views
10

Ich arbeite an Popup-Menü in Actionbar. Aber ich bin fest, um genau unten von actionbar anzuzeigen (cut-to-cut). Ich setze zwei Schnappschüsse.Wie Menü Popup genau unterhalb Aktionsleiste anzeigen?

Mein Problem Screenshot:

enter image description here

ich unten von ActionBar genau Popup-Menü möchten, wie unten Screenshot

Korrektur Screenshot:

enter image description here

Mein Code-Schnipsel:

<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

Bitte keine Screenshots oder Links posten. Poste stattdessen deinen Code – Andrew

+0

Ich habe meinen Code in meine Frage eingefügt. – Vasant

Antwort

4

das Thema bei Android Manifest Set zu

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

Gemäß meinem Code ist dies genau sotution durch Stil zu ändern.

<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

Durch Hinzufügen des Offsets wurde in Android 4.4.4 zusätzlicher Platz zwischen der Symbolleiste und dem Menü geschaffen. – Ankit

+2

Ich habe hier keine Toolbar benutzt, ich habe Actionbar benutzt. – Vasant

+0

danke für einschließlich pre-lollipop zu – Safeer

1

Sie können diese Eigenschaft durch Stil erreichen 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> 

und setzen diesen Stil in AppTheme wie unter

<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" erfordert mindestens API level 21 –

9

Wenn mit AppCompat Thema arbeiten, unter Code help you.Either Kitkat oder Lollipop

Machen Sie Ihre style.xml wie unten

<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

Funktioniert perfekt, Danke mann –

6

Wenn Sie ActionBar und AppCompat Thema halten möchten, mit anstatt ToolBar oder Holo Thema, können Sie diesen Code verwenden:

<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> 

Das funktionierte für mich.

+1

Arbeitete auch für mich. Vielen Dank. – dazed

+0

Arbeitete für mich. Getestet auf Android 4.x, 6.x. Vielen Dank! – hungtdo