繁体   English   中英

与RelativeLayout重叠的ViewPager选项卡

[英]Overlap ViewPager tabs with a RelativeLayout

我有一个Activity,其中包含带有两个标签的ViewPager。 当用户按下按钮时,我希望显示一个不透明的RelativeLayout叠加层,然后用户可以在其中选择不同的选项。

问题在于,覆盖图不会与ViewPager的选项卡重叠,而是会停下来。

我的XML:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:background="@color/light_gray" >

            <TextView
                 ..../>

        </LinearLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/footer"/>

    </RelativeLayout>

    <Button
        .../>

    <RelativeLayout
        android:id="@+id/overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/orange"
        android:visibility="gone"
        android:alpha="0.9">
    </RelativeLayout>

</RelativeLayout>

当前是什么样的:

当前是什么样的:

我希望它看起来像什么:

我想要它看起来像什么

AFAIK选项卡属于ActionBar,您不能仅覆盖选项卡。 但是,如果使用支持库v7,则可以在布局文件中定义工具栏(ActionBar的新名称)。 您还可以在xml中定义TabLayout(标签的容器)。 为此,您必须使用Google的新设计支持库。

只需通过添加导入这些库

    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'

到您的gradle文件。 现在您可以使用

    <android.support.v7.widget.Toolbar />
    <android.support.design.widget.TabLayout />

在您的布局文件中。 确保将主题更新为

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">

因此将没有默认的ActionBar并从AppCompatActivity继承,而不是从ActivityFragmentActivity或其他继承。 为了使您的工具栏像ActionBar一样工作,请按ID加载Toolbar并使用setSupportActionBar(toolbar) 要使ViewPager和TabLayout一起工作,只需照常设置ViewPager,然后在TabLayout对象上调用setupWithViewPager(viewPager)

生成的xml应该如下所示:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.TabLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent" />

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </RelativeLayout>

</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM