开发者

ActionBar Overflow Menu styling information for manual implementation

I'm trying to duplicate the Overflow Menu dropdown functionality for other ActionBar items. I'm working on a manual implementation of this functionality as I think it has been left out of the environment (likely to force standardisation of UI's). Does anybody kn开发者_Go百科ow what style / style items are used for the drop down list when you click on the Overflow menu?

EDIT

The overflow button is actually a modified spinner. Here is the style information for it.

<style name="Widget.Holo.Spinner" parent="Widget.Spinner.DropDown">
    <item name="android:background">@android:drawable/spinner_background_holo_dark</item>
    <item name="android:dropDownSelector">@android:drawable/list_selector_holo_dark</item>
    <item name="android:popupBackground">@android:drawable/menu_dropdown_panel_holo_dark</item>
    <item name="android:dropDownVerticalOffset">0dip</item>
    <item name="android:dropDownHorizontalOffset">0dip</item>
    <item name="android:dropDownWidth">wrap_content</item>
    <item name="android:popupPromptView">@android:layout/simple_dropdown_hint</item>
    <item name="android:gravity">left|center_vertical</item>
</style>


Here is a roundup of what I've cobbled together:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dropdownContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/leftBuffer"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/actionbarDropdown"
            style="@style/Widget.ActionBarDropDown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="@android:color/transparent"
            android:entries="@array/sortOptions" />

        <LinearLayout
            android:id="@+id/bottomBuffer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/rightBuffer"
        android:layout_width="118px"
        android:layout_height="match_parent" />

</LinearLayout>

Adding an onClick to an ActionBar item which: adds the above layout as a child to your Activities root ViewGroup gives you the illusion of a drop down.

Adding an onClick to each of the buffers which removes the view from the root ViewGroup allows the drop down to "exit" when you try and move focus.

The styling information for the drop down is:

<item name="android:background">@drawable/menu_dropdown_panel_holo_light</item>
<item name="android:dropDownSelector">@drawable/list_selector_background</item>

The layout for each list item is:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="48dp"
      android:textSize="17sp"
      android:gravity="right|center_vertical"
      style="?android:attr/dropDownItemStyle"
      android:ellipsize="marquee"
      android:id="@android:id/text1">    
</TextView>

This doesn't give a perfect copy of the functionality of an overflow drop down but it's pretty darn close. I am very interested if anyone else knows a way to reproduce this functionality in a more integrated way!


I achive this by having a simple custom view that I put in the ActionBar:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/action_starred"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    style="?android:attr/actionButtonStyle"
    android:src="@drawable/ic_action_star" />

I then attach a OnClickListener to it in onCreateOptionsMenu() which simply adds a Fragment which takes care of creating a ListPopupWindow and setting the anchor to be the action view. It finds the action view via getActivity().findViewById(R.id.action_starred).

That's simply it, you can set the popup to be modal to make it behave more like a menu. As list items you can use something like android.R.layout.simple_dropdown_item_1line.

This method should work equally well even if you don't put the view in the ActionBar as I do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜