2016-10-22 73 views
0

我遇到此問題:ViewPager中的片段的一部分由容器佈局中的AppBarLayout覆蓋。在以下示例中,我的textview隱藏在選項卡下方。AppBarLayout覆蓋的片段的一部分

sample image

容器:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/content_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="pl.webtube.pracainzynierska.MainActivity" 
tools:showIn="@layout/app_bar_main"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appBarLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:tabMode="fixed" 
     app:tabGravity="fill"/> 
</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:layout_alignParentTop="true" /> 

</RelativeLayout> 

片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:custom="http://schemas.android.com/apk/res-auto" 

tools:context="pl.webtube.pracainzynierska.OneFragment"> 



<TextView 
    android:layout_below="@id/appBarLayout" 
    android:text="Sample text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="25dp" 
    android:id="@+id/textView5" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

在這裏,我填充內部ViewPager片段:

private void setupViewPager(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    adapter.addFragment(new OneFragment(), "ONE"); 
    adapter.addFragment(new TwoFragment(), "TWO"); 
    adapter.addFragment(new ThreeFragment(), "ONE"); 
    viewPager.setAdapter(adapter); 
} 

我試過android:layout_below與AppBarLayout,但它沒有區別。

這是怎麼回事?

回答

1

使用線性佈局,而不是...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/content_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="pl.webtube.pracainzynierska.MainActivity" 
tools:showIn="@layout/app_bar_main"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appBarLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 


    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:tabMode="fixed" 
     app:tabGravity="fill"/> 
</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:layout_alignParentTop="true" /> 

</LinearLayout > 
+0

謝謝!完美的作品 – AndroidBegginer

0

改變你的代碼

<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_alignParentTop="true" /> 

這個......添加機器人:layout_below = 「@ + ID/appBarLayout」這一行在您的ViewPager標籤

<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_alignParentTop="true" 
    android:layout_below="@+id/appBarLayout"/>