簡體   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