的中心我有一個簡單的佈局排列,如下:視圖不在佈局
<?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>
請注意,我用CoordinatorLayout我ViewPager有 app: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停止崩潰並擴展功能。
編輯
在佈局預覽中,我們可以看到,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"
:
沒有app:layout_behavior="@string/appbar_scrolling_view_behavior"
:
EDIT 2
所以,我決定做一個測試,以重現上well referenced 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>
也許是因爲你在一個佈局中使用了太多東西! –
user android:layout_centerInParent =「true」for your textview – Pavya
正如我所說的,app:layout_behavior屬性是需要展開和摺疊工具欄的。刪除它看起來很不錯(TextView位於佈局的中間)。也許我忘了發佈該片段也包含recyclerView(上下滾動的項目列表,可以重現摺疊和展開效果)。 –