Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 视图不';t在布局的中心对齐_Android_Android Layout - Fatal编程技术网

Android 视图不';t在布局的中心对齐

Android 视图不';t在布局的中心对齐,android,android-layout,Android,Android 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

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

<?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>

尝试将
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
添加到片段的布局xml文件中

Edit1
事实上,它的行为与预期相符。 当您的
Viewpager
带有:
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”

Android会根据
AppBarLayout
的高度,向下移动您的
ViewPager
(可能会增加边距)。当您向上滚动视图时,此边距会重新调整。因为这就是
scrolling\u view\u行为
的本质

没有:
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”

您的
ViewPager
独立于
AppBarLAyout
,因此,在本例中,
TextView
显示在中间的原因是没有边距。



所以,在现实中,文本视图始终位于ViewPager的中心。它是移动的浏览页面。要更改该行为,您可能需要实现自己的布局行为。android就是这样做的。

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

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

可能是因为你在一个布局中使用了太多的东西!用户android:layout\u centerInParent=“true”对于您的文本视图正如我所说,app:layout\u行为属性需要展开和折叠工具栏。将其全部删除看起来不错(TextView位于布局的中间)。也许我忘了发布这个片段也包含一个recyclerView(一个上下滚动的项目列表,它再现了折叠和扩展的效果)。如果我删除所有ATIBUTE以居中显示文本视图,它会按预期显示在布局的左上角,但是,只有在尝试将其放置在布局的中心时,才会出现这种奇怪的情况。当我们需要在布局的中心添加一个视图时,应用程序:布局\行为会改变边距…这里有一个类似的问题,建议了一个简单的解决方案:不工作!请参阅上面的最后一次编辑(它显示了预览辅助程序的情况)。在同一预览辅助程序中,使用:
app:layout\u行为=“@string/appbar\u scrolling\u view\u behavior
,您可以检查文本视图在RelativeLayout中的位置吗。你能发布预览吗?也不行!该位置与上一个位置相同,没有此属性。请参阅我的编辑2,它在上演示了相同的问题。感谢您的解释@n.arrow001!但如果您演示如何使用自定义布局行为处理此问题,我将非常感谢您的回答。
<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>
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            container.setPadding(0, 0, 0, appBarLayout.getTotalScrollRange() + verticalOffset);
        ...