Android 与RelativeLayout重叠同级视图
我的应用程序顶部有一个RelativeLayout。在相对位置下方,我有一个ViewPager 为了用一种有意义的方式来解释这一点,假设屏幕的高度是700像素。相对亮度约为200像素高 我希望RelativeLayout绝对位于应用程序的顶部,以便ViewPager位于其后面。然后我想在ViewPager中添加一个200像素的paddingTop,这样它就会显示在RelativeLayout下 这是我现在的布局,显然不起作用:Android 与RelativeLayout重叠同级视图,android,android-layout,android-xml,Android,Android Layout,Android Xml,我的应用程序顶部有一个RelativeLayout。在相对位置下方,我有一个ViewPager 为了用一种有意义的方式来解释这一点,假设屏幕的高度是700像素。相对亮度约为200像素高 我希望RelativeLayout绝对位于应用程序的顶部,以便ViewPager位于其后面。然后我想在ViewPager中添加一个200像素的paddingTop,这样它就会显示在RelativeLayout下 这是我现在的布局,显然不起作用: <LinearLayout xmlns:android
<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>
如何执行此操作?尝试将线性布局更改为框架布局。这将允许将视图放置在彼此的顶部 然后将pageviewer的代码放在relativelayout上方。这将使pageviewer在首先渲染时显示在relativelayout的后面,而relativelayout则显示在其上面。Android将按照视图声明的顺序呈现视图
最后,在pageviewer中添加上边距/填充,使其看起来位于relativelayout下方 以下是您可以做的: 使用RelativeLayout而不是LinearLayout作为根。 将ViewPagerpager视图移动到子RelativeLayoutheader的顶部
<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>
这将使标题保持在固定位置,这意味着如果向下滚动,它将保持在其位置。我需要它与ViewPager一起正常滚动。我应该提到,标题下面是我正在加载的ListView。我在问题中添加了代码。@user5291000如果您想复制twitter配置文件布局,ListView应该放在ViewPager中。其他解决方案也有同样的问题。这将使标题保持在固定位置,这意味着如果我向下滚动,它将保持在其位置。我应该提到,标题下方是我正在加载的ListView。我在问题中添加了代码。很难理解您想要实现的目标。请制作一个简单的模型,用油漆或其他任何东西来说明你想要它的样子,然后将它添加到问题中。你使用Twitter应用程序吗?如果你转到某人的个人资料页面,你会注意到他们在页面的顶部有个人资料信息的布局,如追随者的描述等。下面他们有一个包含该用户帖子的列表视图。您还可以向左或向右滑动以转到下一个ListView推文、照片和收藏夹。垂直滚动时,标题会随着ListView向下滚动。如果向左或向右滚动,标题不会更改,但ListView内容会更改。这就是我想要复制的。Twitter应用程序的图像:
<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>