2016-09-13 115 views
1

的中心我有一個簡單的佈局排列,如下:視圖不在佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:text: "NOT CENTRALIZED!" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="gone" /> 
</RelativeLayout> 

上述佈局屬於哪個是ViewPager(屬於一個活動裏面加載的碎片 - 稱爲在這裏作爲ActivityA.java)。

的ActivityA.java 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: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:stateListAnimator="@animator/elevated_appbar" 
    app: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" 
      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="?attr/actionBarSize" 
      app:tabIndicatorColor="@android:color/white" 
      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> 

請注意,我用CoordinatorLayoutViewPagerapp:layout_behavior="@string/appbar_scrolling_view_behavior"屬性。

的小東西,這讓我困惑的是:爲什麼,因爲我聲明瞭android:layout_centerVertical="true"android:layout_centerVertical="true"它我的TextView未在佈局集中?

奇怪的是,當我在刪除app:layout_scrollFlags="scroll|enterAlways"屬性android.support.v4.view.ViewPager顯示在屏幕中間的內容,但是......該android.support.v7.widget.Toolbar停止崩潰並擴展功能。

Demonstrating the issue - image 1

編輯

在佈局預覽中,我們可以看到,android.support.v4.view.ViewPager不是導航欄上方(但需要app:layout_behavior="@string/appbar_scrolling_view_behavior"把這個觀點波紋管的android.support.design.widget.AppBarLayout)。刪除此屬性時,android.support.v4.view.ViewPager在導航欄上方,但與android.support.design.widget.AppBarLayout重疊。

請看:

隨着app:layout_behavior="@string/appbar_scrolling_view_behavior"

Demonstrating the issue - image 2

沒有app:layout_behavior="@string/appbar_scrolling_view_behavior"

Demonstrating the issue - image 3

EDIT 2

所以,我決定做一個測試,以重現上well referenced project這表明一些材料的特性和結果是這個問題...

Demonstrating the issue - cheesesquare project

...同樣的問題!

如果有人想給自己重現,只改變fragment_cheese_list.xml到:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="gone" /> 

<android.support.v7.widget.AppCompatTextView 
    android:id="@+id/alertMessage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:text="ALSO, NOT CENTRALIZED..." /> 
</RelativeLayout> 
+1

也許是因爲你在一個佈局中使用了太多東西! –

+1

user android:layout_centerInParent =「true」for your textview – Pavya

+0

正如我所說的,app:layout_behavior屬性是需要展開和摺疊工具欄的。刪除它看起來很不錯(TextView位於佈局的中間)。也許我忘了發佈該片段也包含recyclerView(上下滾動的項目列表,可以重現摺疊和展開效果)。 –

回答

0

嘗試增加app:layout_behavior="@string/appbar_scrolling_view_behavior"你的碎片的佈局XML文件。

EDIT1
實際上,因爲它被認爲是道德的。 當你有你Viewpager有:app:layout_behavior="@string/appbar_scrolling_view_behavior"

的Android移動你的ViewPager下,根據AppBarLayout高度(可能是增加保證金的話)。當您向上滾動視圖時,此邊距會重新調整。因爲這是scrolling_view_behavior

enter image description here

而且沒有:app:layout_behavior="@string/appbar_scrolling_view_behavior"

ViewPager是獨立的AppBarLAyout因此,無餘量這就是爲什麼你TextView出現在中心在這種情況下。
enter image description here


所以,在現實中,你的TextView總是在你ViewPager的中心。它的ViewPager發生了變化。爲了改變這種行爲,你可能需要實現你自己的佈局行爲。 This是如何做到這一點。

+0

不起作用!查看上面的最後一個編輯(它顯示預覽助手發生了什麼)。 –

+0

在相同的預覽助手,與:'應用程序:layout_behavior =「@字符串/ appbar_scrolling_view_behavior',你可以檢查您的文本視圖的位置,在RelativeLayout。您可以張貼預覽? –

+0

也不工作!位置是相​​同的因爲前面沒有這個屬性,請參閱我的編輯2,它在[cheesesquare project](https://github.com/chrisbanes/cheesesquare)上演示了相同的問題 –