Android ViewPager导致错误的布局显示

Android ViewPager导致错误的布局显示,android,android-layout,android-viewpager,Android,Android Layout,Android Viewpager,我已在具有以下布局的活动中添加了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">

我已在具有以下布局的活动中添加了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已经移到了屏幕的中央。

这样试试。只需指定元素的高度

<?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>

  • 是否需要将工具栏放在框架布局中。如果是这样的话,考虑删除工具栏的WIWE,只留下WrAPX内容。
  • 更改布局的高度,将重量指定为“0dip”
  • 如果其他的都失败,考虑将Wistes改为整数,并将它们添加到线性布局的WistySand中,例如工具栏帧布局得到9,视图寻呼机得到3,底部的帧得到8,加权和是20。
    使用
    RelativeLayout
    很容易实现布局。使用
    RelativeLayout
    并将
    工具栏布局
    设为顶部子级,并将
    ViewPager
    置于“工具栏布局”下方

    希望这有帮助

    请注意,

    尝试此布局

     <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>
    
    
    
    您希望如何显示工具栏和viewpager?我希望工具栏位于屏幕顶部,而viewpager正好位于屏幕下方。我已经找到了答案。将viewpager放置在linearlayout中可使其正常工作。如果完全使用linearlayout,则如果希望其全屏显示,则FrameLayout容器将毫无用处。但是,如果您只希望在线性布局中具有指定的重量,则可以。