在协调布局中,Viewpager超出屏幕范围

8
我正在尝试使用 design support library 来实现 选项卡。问题在于,当我去掉属性时,viewpager 会覆盖 toolbartablayout
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 

关于viewpager的问题。

viewpager与工具栏和标签布局重叠

当我添加属性时,viewpager会溢出屏幕。

viewpager溢出了屏幕

我只需要viewpager占用标签布局和工具栏之间剩余的空间,不要溢出屏幕……

这是我的activity_main.xml

<android.support.design.widget.CoordinatorLayout 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.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed" />
   </android.support.design.widget.AppBarLayout>

  <android.support.v4.view.ViewPager
     android:id="@+id/viewpager"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

这是我的fragment_1.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

  <TextView
    android:id="@+id/tV_item1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/activity_horizontal_margin"
    android:gravity="center"
    android:text="@string/tab1_q_persons"
    android:textSize="20sp"
    android:textStyle="bold" />

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="0 people"
    android:textSize="30sp"
    android:textStyle="bold" />

  <ImageView
    android:id="@+id/iV_plus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

  <ImageView
    android:id="@+id/iV_minus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <Button
        android:id="@+id/nextBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="end"
        android:background="@color/colorPrimary"
        android:elevation="12dp"
        android:text="@string/next" />

 </RelativeLayout>

</LinearLayout>

非常感谢提供帮助

2个回答

1
我知道看起来有点晚了,但是我还是发送我的答案。
在activity_main.xml中使用RelativeLayout代替CoordinatorLayout,您可以删除AppBar。要将Toolbar定义为AppBar,您可以在MainActivity.java中使用此代码:
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);

在fragment_1.xml中,使用类似CardView或ScrollView的其他小部件,而不是LinearLayout,或者您可以在替换的小部件内部使用LinearLayout。

activity_main.xml

<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.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_alignParentTop="true"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed" 
        android:layout_below="@id/toolbar"/>

  <android.support.v4.view.ViewPager
     android:id="@+id/viewpager"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:layout_below="@id/tablayout" />

</RelativeLayout>

fragment_1.xml

<android.support.v7.widget.CardView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

      <TextView
        android:id="@+id/tV_item1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:gravity="center"
        android:text="@string/tab1_q_persons"
        android:textSize="20sp"
        android:textStyle="bold" />

      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="0 people"
        android:textSize="30sp"
        android:textStyle="bold" />

      <ImageView
        android:id="@+id/iV_plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

      <ImageView
        android:id="@+id/iV_minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/nextBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="end"
            android:background="@color/colorPrimary"
            android:elevation="12dp"
            android:text="@string/next" />

     </RelativeLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

这个解决方案对我也解决了同样的问题。

0
如果您不需要 AppBar Layout,可以将其删除。
<android.support.design.widget.CoordinatorLayout 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.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed" />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

这段代码对我来说运行良好。


1
我认为我需要使用AppBar布局,否则工具栏和选项卡会重叠。 - Joel Raju
将 android:layout_below="@+id/toolbar" 添加到您的 TabLayout。 - Anuraag Baishya
你能告诉我添加 Appbar 布局的实际用途是什么吗?我读到它与 Linearlayout 相似。 - Joel Raju

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接