8

我試圖使用設計支持庫來實現選項卡。問題是viewpager與工具欄和tablayout重疊,當我刪除viewpager的屬性Viewpager溢出協調器佈局內的屏幕

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

viewpager overlaps the toolbar and tablayout

,當我添加屬性的viewpager溢出屏幕。

viewpager overflows the screen

我需要的只是viewpager佔據tablayout後留下的空間和工具欄,而不是溢出屏幕...

這裏是我的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> 

任何幫助將不勝感激

回答

0

如果你不真正需要的AppBar佈局,將其刪除。

<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佈局,否則**工具欄和Tablayout ovwelaps **。 –

+0

添加android:layout_below =「@ + id/toolbar」到你的TabLayout –

+0

你能告訴我添加** Appbar **佈局的實際用途是什麼。我讀它與** Linearlayout **類似。 –

1

我知道似乎晚了。但我發送了我的答案。

在activity_main.xml中使用RelativeLayout而不是CoordinatorLayout,您可以刪除AppBar。爲了定義工具欄作爲AppBar你可以在MainActivity.java使用此代碼:

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(mToolbar); 

在fragment_1.xml使用其他窗口一樣CardView或滾動型,而不是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> 

該解決方案解決了同樣的問題對我來說。