Android:使操作栏重叠Activity布局

8

我有一个活动,其中Actionbar被隐藏,当我点击按钮时,我想让它可见,但我不希望布局活动向下滚动,该栏必须重叠在其上。我该如何做到这一点?

要隐藏栏,我使用以下代码:

actionbar.hide();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

并将其可见:

actionbar.show();
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

我的布局只是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <MyPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <MyPager>

</RelativeLayout>

提前致谢。


请粘贴一些代码或布局,仅根据此描述很难表达任何内容。 - Egor
3个回答

32

你可能正在寻找 ActionBar 的覆盖模式。在调用 setContentView() 之前,通过调用 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 来为单个 Activity 启用它。或者在主题中将 android:windowActionBarOverlay 设置为 true


太棒了,正是我所需要的。 - e_ori
2
在我的情况下,我还必须在super.onCreate(savedInstanceState);之前放置requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); - Soren Stoutner

0
创建一个RelativeLayout作为活动的父布局,并将您的操作栏放置在布局的顶部,覆盖在主活动内容的顶部,如下面的示例所示。
RelativeLayout允许您定位元素,还允许您通过从上到下的顺序确定视图元素的z-index顺序,即哪些视图在其他视图的前面/后面。
<RelativeLayout
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/action_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="100dp">

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

    </LinearLayout>
</RelativeLayout>

现在您可以隐藏/显示action_bar_layout,而不会影响main_activity_content_layout中活动内容的位置(通过使用布局的setVisibility()方法)。

0

要实现类似Google Play商店的效果,请尝试...

  • 尝试使用Lollipop(API Level 21)中引入的this新工具栏小部件
  • 该小部件也支持this API在较早版本的安卓系统上。

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