简体   繁体   English

与RelativeLayout重叠的同级视图

[英]Overlap sibling view with RelativeLayout

I have a RelativeLayout at the top of my app. 我的应用程序顶部有一个RelativeLayout。 Below the RelativeLayout I have a ViewPager. 在RelativeLayout下面,我有一个ViewPager。

To explain this in a way that will make sense, imagine the screen's height is 700 pixels. 为了以有意义的方式进行解释,请假设屏幕的高度为700像素。 The RelativeLayout is about 200 pixels high. RelativeLayout的高度约为200像素。

I want the RelativeLayout to be position absolutely at the top of the app such that the ViewPager is behind it. 我希望RelativeLayout绝对位于应用程序的顶部,以使ViewPager在它的后面。 I then want to add a 200 pixel paddingTop to the ViewPager so that it appears under the RelativeLayout. 然后,我想向ViewPager添加一个200像素的paddingTop以使其出现在RelativeLayout下。

Here is the layout I have now (which is obviously not working): 这是我现在拥有的布局(显然不起作用):

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

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        ...

    </RelativeLayout>

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

</LinearLayout>

I load a ListView with some data under the header : 我在header下面加载了一些数据的ListView:

<LinearLayout
    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">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

How can I do this? 我怎样才能做到这一点?

Try changing the linearlayout to a framelayout. 尝试将linearlayout更改为framelayout。 This will allow the views to be placed on top of each other. 这将允许视图彼此重叠。

Then put the code for the pageviewer above the relativelayout. 然后,将pageviewer的代码放在相对布局上方。 This will make the pageviewer appear to be behind the relativelayout as it is rendered first, and the relativelayout rendered on top of it. 这将使网页浏览器在第一次呈现时显得在相对布局的后面,而相对布局则在其顶部呈现。 Android will render views in the order in which they are declared. Android将按照声明的顺序渲染视图。

Lastly, add a top margin/padding to the pageviewer to make it seem positioned below the relativelayout. 最后,在网页浏览器中添加上边距/内边距,使其看起来位于相对布局下方。

Here's what you can do: 您可以执行以下操作:

  • Use RelativeLayout instead of LinearLayout as the root. 使用RelativeLayout而不是LinearLayout作为根。
  • Move the ViewPager(pager) view to the top of child RelativeLayout(header) 将ViewPager(pager)视图移动到子RelativeLayout(标题)的顶部

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:paddingTop="200px" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="wrap_content"> </RelativeLayout> </RelativeLayout> 

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

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