手动实现ActionBar溢出菜单样式信息

4
我正在尝试为其他ActionBar项复制Overflow菜单下拉功能。我正在手动实现此功能,因为我认为它已被忽略(可能是为了强制UI标准化)。有人知道在单击Overflow菜单时使用的样式/样式项是什么吗?
编辑:
溢出按钮实际上是一个修改过的spinner。这是它的样式信息。
<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>
2个回答

2

这里是我所整理的内容概述:

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

给ActionBar项添加一个onClick事件,将上述布局作为子元素添加到您的Activity根ViewGroup中,从而给您营造出下拉菜单的错觉。

为每个缓冲区添加一个onClick事件,从根ViewGroup中删除视图可以使下拉菜单在尝试移动焦点时“退出”。

下拉菜单的样式信息如下:

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

每个列表项的布局如下:
<?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>

这并不能完美地复制溢出下拉功能,但非常接近。如果有人知道以更集成的方式重现此功能,我会很感兴趣!

"@style/Widget.ActionBarDropDown"这个样式是如何定义的?我无法解析它。 - Henrik

0
我通过在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" />

然后我在onCreateOptionsMenu()中将OnClickListener附加到它上面,它只是添加了一个Fragment,该Fragment负责创建ListPopupWindow并将锚点设置为操作视图。它通过getActivity().findViewById(R.id.action_starred)找到操作视图。

就是这样简单,您可以将弹出窗口设置为模态以使其更像菜单。作为列表项,您可以使用类似于android.R.layout.simple_dropdown_item_1line的东西。

即使您不像我一样将视图放在ActionBar中,此方法也应该同样有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接