Android 丑虫子?RelativeLayout中的第一项不能居中

Android 丑虫子?RelativeLayout中的第一项不能居中,android,layout,Android,Layout,我们尝试一件简单的事情。在相对位置水平和垂直居中显示文本视图 这应该是 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent"> <

我们尝试一件简单的事情。在相对位置水平和垂直居中显示文本视图

这应该是

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

与所有预期相反,文本显示在左上角

但现在在整个过程中添加一个完全没有意义的线性布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- First item in a relative Layout cannot be centered  -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
         >
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

还有tadaaaa!文本显示在我们希望他显示的地方。如果不更改TextView的位置,则无法删除LinearLayout中的任何行。似乎RelativeLayout需要将一些项目与上/下和右/左对齐,然后才能将其他项目居中。

类似的问题? 问题描述 我目前有一个类似的问题,那就是包装
RelativeLayout
时持有一个
文本视图
,由于使用了
android:layout\u centerInParent=“true”
,该视图应该居中

在API level 21设备或仿真器上运行我的代码时,文本将正确居中显示

但是,在API级别19上运行相同的代码会将文本视图置于包装RelativeLayout的顶部,而不是居中

示例项目 为了便于后续工作,我在上创建了一个示例项目,只需从git中查看此代码并在AndroidStudio中导入,您就应该能够运行此代码

想象 您甚至可以在androidstudio中使用设计预览来测试这个问题,只需更改用于渲染的SDK版本

只要在
layout/with_scrollview.xml
打开文件,您就可以在Android Studio的预览中看到它

使用API级别21:

使用API级别19的相同布局:

变通 包装滚动视图 作为一种解决方法,我发现移除包装滚动视图可以解决问题

什么没用 虚拟视图 像你建议的那样在中间添加一个虚拟的
线性布局
(或任何其他视图),并不能解决我的问题


示例项目在屏幕右侧的圆圈中应用了此技术。

为什么不使用
android:layout\u centerInParent=“true”
?另外,您使用的是什么SDK/ADT?行为与centerInParent相同。我正在使用Build:v21.1.0-569685第一个代码在垂直和水平中心正确显示文本。在IntelliJ iDeaar中测试您是否实际运行此代码?或者你要看Eclipse中的预览?如果你正在运行这个代码,你在什么上面运行它?