【发布时间】:2017-01-20 14:23:09
【问题描述】:
我有一个简单的布局,如下:
<?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 内的 Fragment(属于 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.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":
编辑 2
因此,我决定在well referenced project 上进行测试以重现此问题,该well referenced project 演示了一些 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>
【问题讨论】:
-
可能是因为你在一个布局中使用了太多东西!
-
用户 android:layout_centerInParent="true" 为您的文本视图
-
如我所说,app:layout_behavior 属性是需要展开和折叠工具栏的。删除它看起来不错(TextView 位于布局的中间)。也许我忘了发布该片段也包含一个 recyclerView(一个上下滚动的项目列表,它再现了折叠和展开效果)。
-
好吧,android:layout_centerInParent="true" 不起作用。如果我删除所有属性以使我的 textView 居中,它会按预期进入布局的左上角,但是,只有在尝试将其放在布局的中心时才会发生这种奇怪的事情。看起来 app:layout_behavior 在我们需要将视图添加到布局中心时会更改边距...
-
这里有一个类似的问题,建议使用简单的解决方案:stackoverflow.com/a/47479847/5536197