[英]View doesn't aligned in the center of layout
我有一個簡單的布局,如下所示:
<?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>
上面的布局屬於一個 Fragment,它被加載到一個 ViewPager(它屬於一個 Activity——這里稱為 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"
屬性。
讓我感到困惑的小事是:為什么我的 TextView 沒有集中在布局上,因為我向它聲明了android:layout_centerVertical="true"
和android:layout_centerVertical="true"
?!
奇怪的是,當我刪除android.support.v4.view.ViewPager上的app:layout_scrollFlags="scroll|enterAlways"
屬性時,內容顯示在屏幕中間,但是...... android.support.v7.widget。工具欄停止折疊和展開功能。
編輯
在布局預覽中,我們可以看到android.support.v4.view.ViewPager
不在導航欄上方(但是需要app:layout_behavior="@string/appbar_scrolling_view_behavior"
將該視圖放在android.support.design.widget.AppBarLayout
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"
:
編輯 2
所以,我決定做一個測試來在一個很好的參考項目中重現這個問題,該項目展示了一些 Material 特性,結果是......
... 同樣的問題!
如果有人想自己復現,只需將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>
嘗試將app:layout_behavior="@string/appbar_scrolling_view_behavior"
到 Fragment 的布局 xml 文件中。
編輯1
實際上,它的行為符合預期。 當你有你的Viewpager
時: app:layout_behavior="@string/appbar_scrolling_view_behavior"
Android的轉變你的ViewPager
下,根據高度AppBarLayout
(可能是增加保證金的話)。 當您向上滾動視圖時,會重新調整此邊距。 因為這就是scrolling_view_behavior
沒有: app:layout_behavior="@string/appbar_scrolling_view_behavior"
您的ViewPager
獨立於AppBarLAyout
因此,在這種情況下,沒有邊距就是您的TextView
出現在中心的原因。
因此,實際上,您的 TextView 始終位於 ViewPager 的中心。 它的 ViewPager 發生了變化。 要更改該行為,您可能需要實現自己的布局行為。 這就是安卓的做法。
我發現的最簡單且不引人注目的解決方法是在 onOffsetChanged 上為您的容器設置底部填充。 這看起來像
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
container.setPadding(0, 0, 0, appBarLayout.getTotalScrollRange() + verticalOffset);
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.