ViewPager导致错误的布局显示

时间:2016-01-12 12:52:48

标签: android android-layout android-viewpager

enter image description here

我在一个具有以下布局的活动中添加了viewpager:

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.45">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.1" />
    </FrameLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/imgdetail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.15"
        android:foregroundGravity="center_vertical|fill_vertical">
    </android.support.v4.view.ViewPager>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container1"
        android:layout_weight="0.4"></FrameLayout>

</LinearLayout>

但工具栏颜色已移动,并且viewpager已移至屏幕中心。

4 个答案:

答案 0 :(得分:1)

以这种方式试试。只需指定元素的高度

即可
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
    android:id="@+id/imgdetail"
    android:layout_below="@id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foregroundGravity="center_vertical|fill_vertical">
</android.support.v4.view.ViewPager>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/container1"
    android:layout_below="@id/imgdetail">

</FrameLayout></RelativeLayout>

答案 1 :(得分:1)

  1. 您是否需要将工具栏放在框架布局中。如果是这样,请考虑删除工具栏的权重,然后保留wrap_content。
  2. 将指定重量的布局高度更改为“0dip”。
  3. 如果所有其他方法都失败,请考虑将wights更改为整数并添加它们以使linear_layout egtoolbar框架布局的wight_sum获得9,视图寻呼机获得3,底部的框架获得8,weight_sum为20。

答案 2 :(得分:1)

使用RelativeLayout轻松实现布局。使用RelativeLayout并将ToolBar layout设为顶级孩子,并将ViewPager放在“工具栏布局”下方。

希望这有帮助。

此致

答案 3 :(得分:1)

试试这个布局

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/imgdetail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"/>

    <FrameLayout
        android:id="@+id/container1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"/>

</FrameLayout>