Android如何创建类似于Google+应用程序的覆盖下拉菜单



我正在尝试创建一个类似Google+应用程序中的下拉菜单。屏幕截图如下。我看过Spinners和Popup Menus,但它们都不适合我想要创建的内容。第一个图像显示关闭的菜单,第二个图像显示打开下拉菜单时的样子。

http://www.droid-life.com/wp-content/uploads/2014/05/google-plus-4.4-1.jpg

菜单不应出现在操作栏中,滚动时,显示所选选项的菜单仍位于屏幕顶部。

很抱歉,我没有足够的声誉来添加评论,所以我会发布一个答案,希望它能帮助任何人。这只是我的另一个解决方案,试图实现你发布的类似谷歌+应用程序的用户界面。

我目前的解决方案是在操作栏下面使用一个工具栏(它也是一个设置为操作栏的工具栏)。然后我为工具栏添加onClickListener。当点击工具栏时,将显示一个回收视图。回收视图可以由布局中的动态数据或自定义数据填充。示例代码:

main_layout.xml

<LinearLayout .... >
// the actionbar toolbar
<android.support.v7.widget.Toolbar
 android:id="@+id/action_toolbar"
 android:layout_width="match_parent"
 android:layout_height="?android:attr/actionBarSize"
        app:theme="@style/toolbarStyle"/>
// second toolbar act as the spinner
<android.support.v7.widget.Toolbar
 android:id="@+id/dropdown_toolbar"
 android:layout_width="match_parent"
 android:layout_height="?android:attr/actionBarSize"
 style="@style/dropdownToolbar">
..... // add a spinner indicator (imageview)
</android.support.v7.widget.Toolbar>
//separator line
<View
 android:id="@+id/separator_line"
 android:layout_width="match_parent"
 android:layout_height="1dp"
 android:layout_marginLeft="0dp"
 android:layout_marginRight="0dp"
 android:background="#adacad"/>
// two child overlaps in framelayout
<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">
// the visible layout example
        <android.support.v7.widget.RecyclerView
            android:id="@+id/default_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
// the layout that is visible when toolbar is tapped
// this is spinner content, put anything u want here
        <android.support.v7.widget.RecyclerView
            android:id="@+id/dropdown_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"/>
</LinearLayout>

这将给你一个完整的宽度和长度布局。dropdown_recycerview内容取决于您想要什么。我添加了一个图像视图来指示假微调器是打开还是关闭。java:的示例代码

主要活动.java

//action toolbar
Toolbar actionToolbar = (Toolbar) findViewById(R.id.action_toolbar);
setSupportActionBar(actionToolbar);
RecyclerView dropdownRecyclerView = (RecyclerView) findViewById(R.id.dropdown_recyclerview);
//second toolbar
Toolbar dropdownToolbar = (Toolbar) findViewById(R.id.dropdown_toolbar);
//Listen for toolbar onClick
dropdownToolbar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
//toggle visibility of dropdown spinner
  if (!SHOW_SPINNER_FLAG) {
//set indicator close / open
  dropdownRecyclerView.setVisibility(View.VISIBLE);
} else {
//set indicator close / open
dropdownRecyclerView.setVisibility(View.VISIBLE);
     } 
   }
});

稍后,您可以自定义布局并添加一些动画以显示假微调器布局。如果还有其他方式,请分享。目前,这是我在应用程序中使用的,对我来说很好。很抱歉我的英语不好。

示例应用程序图像

最新更新