2016-01-26 89 views
1

我創建了一個Simple ViewPager項目。我試圖在ViewPager上方添加一個標準標題(CardView),並在Viewpager下方添加兩個按鈕。不顯示添加CardView或任何其他視圖(TextView)。 ViewPager只能說明 這是我的默認佈局與我的意見(嘗試)...Viewpager的靜態頁眉和頁腳

In My Mind

activity_home.xml

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout 
    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/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context="grabit.viewpageronly.MainActivity"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="@dimen/appbar_padding_top" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <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/AppTheme.PopupOverlay"> 

      </android.support.v7.widget.Toolbar> 

     </android.support.design.widget.AppBarLayout> 
    <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="end|bottom" 
      android:layout_margin="@dimen/fab_margin" 
      android:src="@android:drawable/ic_dialog_email" /> 

    <!-- I Can't Add any views here if I added anything like TextView it doesn't Show up --> 
    <!-- Some Standard CardView --> 

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

     <!-- Two Button below Viewpager --> 

    </android.support.design.widget.CoordinatorLayout> 

這裏我已經試過

<?xml version="1.0" encoding="utf-8"?> 
     <android.support.design.widget.CoordinatorLayout> 
      <appbar ... /> 
       <toolbar .... /> 

    <TextView android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Hello World"/> 

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

     </android.support.design.widget.CoordinatorLayout> 
+1

你**的TextView **去在你的** ** ViewPager,把它們的LinearLayout和你的問題將被修復 – Amir

+0

是啊我已經嘗試用LinearLayout和RelativeLayout封閉我的textview和viewpager,在RelativeLayout中我將viewpager放在textview下(android:layout_below =「@ + id/textview」) –

+0

@sathsh不起作用? – Amir

回答

1

問題是,您應該將行爲添加到您的LinearLayout而不是您的ViewPager

以下XML的作品,它是在頂部添加的TextView用紅色背景的viewPager的:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     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:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:layout_scrollFlags="scroll|enterAlways|snap" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </android.support.design.widget.AppBarLayout> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <TextView 
     android:background="#f22" 
     android:id="@+id/tes" 
     android:text="this is test" 
     android:textColor="#fff" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

</LinearLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_done" /> 

</android.support.design.widget.CoordinatorLayout> 
+1

你救了我的一天! , 非常感謝 :-) –