gpt4 book ai didi

android - View 未在布局中心对齐

转载 作者:太空狗 更新时间:2023-10-29 13:12:01 24 4
gpt4 key购买 nike

我有一个简单的布局,如下:

<?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"对吗?!

奇怪的是,当我删除 app:layout_scrollFlags="scroll|enterAlways" android.support.v4.view.ViewPager 属性内容显示在屏幕中间,但是... android.support.v7.widget.Toolbar停止折叠和展开功能。

Demonstrating the issue - image 1

编辑

在布局预览中,我们可以看到 android.support.v4.view.ViewPager不在导航栏上方(但需要 app:layout_behavior="@string/appbar_scrolling_view_behavior" 才能将此 View 放在 android.support.design.widget.AppBarLayout 下方)。删除此属性时,android.support.v4.view.ViewPager向上导航栏,但与 android.support.design.widget.AppBarLayout 重叠.

看看:

app:layout_behavior="@string/appbar_scrolling_view_behavior" :

Demonstrating the issue - image 2

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

Demonstrating the issue - image 3

编辑 2

因此,我决定进行测试以在 well referenced project 上重现此问题。它演示了一些 Material 特性,结果是......

Demonstrating the issue - cheesesquare 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>

最佳答案

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

编辑1
实际上它的行为就像它应该的那样。当您的 Viewpager 具有:app:layout_behavior="@string/appbar_scrolling_view_behavior"

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

enter image description here

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

您的 ViewPager 独立于 AppBarLAyout 因此,在这种情况下,您的 TextView 毫无保留地出现在中心。
enter image description here


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

关于android - View 未在布局中心对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com