Android 被嵌套的RelativeLayout设置paddingTop后出现问题

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="17dp"
    android:background="#00ffff"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textColor="?attr/text_color_black_academia"
        android:textSize="17sp"
        android:text="@string/test" />
</RelativeLayout>

这个布局显示的结果:

然后,在RelativeLayout的外层包裹一个LinearLayout,即:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="17dp"
        android:background="#00ffff"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:textColor="?attr/text_color_black_academia"
            android:textSize="17sp"
            android:text="@string/test" />
    </RelativeLayout>
</LinearLayout>

这时候显示的结果是:

 这是问题,但目前还不太懂是怎么回事。值得注意的是,TextView的android:layout_centerVertical="true"这个属性是问题的核心,因为如果去掉这个属性,就能正常显示了。

猜你喜欢

转载自blog.csdn.net/yeziyfx/article/details/122327664
今日推荐