【问题标题】:View doesn't aligned in the center of layout视图未在布局中心对齐
【发布时间】: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

标签: android android-layout


【解决方案1】:

尝试将 app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到 Fragment 的布局 xml 文件中。

编辑1
实际上它的行为是应该的。 当您拥有Viewpager 时:app:layout_behavior="@string/appbar_scrolling_view_behavior"

Android 会根据AppBarLayout 的高度向下移动您的ViewPager(可能会为其添加边距)。当您向上滚动视图时,此边距会重新调整。因为这就是 scrolling_view_behavior 的含义

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

您的ViewPager 独立于AppBarLAyout 因此,没有边距这就是为什么您的TextView 在这种情况下出现在中心。


因此,实际上,您的 TextView 始终位于 ViewPager 的中心。它是改变的 ViewPager。要更改该行为,您可能需要实现自己的布局行为。 This 是 android 的做法。

【讨论】:

  • 不起作用!请参阅上面的最后一个编辑(它显示了预览助手发生的情况)。
  • 在同一个预览助手中,带有:app:layout_behavior="@string/appbar_scrolling_view_behavior,你可以在RelativeLayout中检查你的文本视图的位置。你能发布那个预览吗?
  • 也不行!位置和前面一样,没有这个属性。请参阅我的编辑 2,它在 cheesesquare project 上演示了相同的问题。
  • 感谢您的解释@n.arrow001!但是,如果您演示如何使用自定义布局行为来处理此问题,我将非常感谢您的回答。
【解决方案2】:

我发现的最简单且不显眼的解决方法是在 onOffsetChanged 上为您的容器设置底部填充。这看起来像

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            container.setPadding(0, 0, 0, appBarLayout.getTotalScrollRange() + verticalOffset);
        ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多