[英]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.