【问题标题】:Android how to create overlay drop-down menu similar to Google+ appAndroid如何创建类似于Google+应用的覆盖下拉菜单
【发布时间】:2014-10-27 01:56:56
【问题描述】:

我正在尝试创建一个类似于 Google+ 应用中的下拉菜单。可以在下面找到屏幕截图。我看过 Spinners 和 Popup Menus,但这些都不完全适合我想要创建的内容。第一张图片显示关闭的菜单,第二张显示下拉菜单打开时的样子。

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

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

【问题讨论】:

  • 也许这可以帮助你:stackoverflow.com/a/9847763/3864698
  • 我确实看过那个例子和许多其他例子;但是,我找不到菜单可滚动的示例,选择将保持覆盖在顶部,并且与屏幕一样宽。
  • 您找到某种解决方案了吗?我面临同样的任务
  • @kononger 我没有。不幸的是,我能想到的最佳选择就是将微调器放在操作栏内。
  • @DonnieAdams,我正在研究自定义解决方案,我认为需要 2-3 天

标签: android


【解决方案1】:

对不起,我没有足够的声誉来添加 cmets,所以我会发布作为答案,希望它对任何人都有帮助。这只是我尝试实现像您发布的 google plus 应用程序这样的 ui 的替代解决方案。

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

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_recyclerview 内容取决于你想要什么。我添加了一个图像视图来指示假微调器是打开还是关闭。 java的示例代码:

MainActivity.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);
     } 
   }
});

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

example app image

【讨论】:

  • 您可以通过 UIAutomatorViewer(在您的 sdk/tools 文件夹中)检查 Google+ 应用程序。他们所做的与你所做的并没有太大的不同。下拉菜单是一个带有 ListView 的 RelativeLayout(所以按钮可以在底部)。
猜你喜欢
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 2012-01-10
相关资源
最近更新 更多