简体   繁体   English

与RelativeLayout重叠的ViewPager选项卡

[英]Overlap ViewPager tabs with a RelativeLayout

I have an Activity which contains a ViewPager with two tabs. 我有一个Activity,其中包含带有两个标签的ViewPager。 When the user presses a button, I want an opaque RelativeLayout overlay to appear where the user can then select different options. 当用户按下按钮时,我希望显示一个不透明的RelativeLayout叠加层,然后用户可以在其中选择不同的选项。

The problem is that the overlay does not overlap the ViewPager's tabs and instead stops short. 问题在于,覆盖图不会与ViewPager的选项卡重叠,而是会停下来。

My XML: 我的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>

What it kind of looks like currently: 当前是什么样的:

当前是什么样的:

What I want it to look like: 我希望它看起来像什么:

我想要它看起来像什么

AFAIK the tabs belong to the ActionBar and you can't overlay just the tabs. AFAIK选项卡属于ActionBar,您不能仅覆盖选项卡。 But if you use the Support Library v7 you can define your Toolbar (new name for ActionBar) in your layout file. 但是,如果使用支持库v7,则可以在布局文件中定义工具栏(ActionBar的新名称)。 You can also define a TabLayout (container for the tabs) in your xml. 您还可以在xml中定义TabLayout(标签的容器)。 To do so you have to use Google's new Design Support Library. 为此,您必须使用Google的新设计支持库。

Just import these libraries by adding 只需通过添加导入这些库

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

to your gradle file. 到您的gradle文件。 Now you can use 现在您可以使用

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

in your layout file. 在您的布局文件中。 Be sure to update your theme to something like 确保将主题更新为

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

so that there will be no default ActionBar and inherit from AppCompatActivity instead of Activity , FragmentActivity or something else. 因此将没有默认的ActionBar并从AppCompatActivity继承,而不是从ActivityFragmentActivity或其他继承。 To get your Toolbar acting like the ActionBar load the Toolbar by its ID and use setSupportActionBar(toolbar) . 为了使您的工具栏像ActionBar一样工作,请按ID加载Toolbar并使用setSupportActionBar(toolbar) To get the ViewPager and TabLayout working together just setup your ViewPager as usually and then invoke setupWithViewPager(viewPager) on the TabLayout object. 要使ViewPager和TabLayout一起工作,只需照常设置ViewPager,然后在TabLayout对象上调用setupWithViewPager(viewPager)

Your resulting xml should look like this: 生成的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