Android ViewPager导致错误的布局显示
我已在具有以下布局的活动中添加了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">
<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>
使用
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容器将毫无用处。但是,如果您只希望在线性布局中具有指定的重量,则可以。