微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android-RelativeLayout中的layout_centerInParent不在模拟器或设备上居中

我有一个这样的布局片段:

<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:text="My Fragment"
        android:textSize="30sp"
        android:layout_centerInParent="true"/>
</RelativeLayout>

在Android Studio预览中,我看到了以下内容

Enter image description here

很好,但是在模拟器或真实设备上,我看到了:

Enter image description here

我想念什么?如何在父母中居中显示该文本?

父布局是FrameLayout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"/>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/frame_layout">

    </FrameLayout>
</LinearLayout>
最佳答案
您需要更改FrameLayout的宽度

将您的FrameLayout宽度更改为android:layout_width =“ match_parent”

样本代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame_layout">

    </FrameLayout>
</LinearLayout>

原文地址:https://www.jb51.cc/android/531370.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐