Android在顶部居中对齐文本失败

Android在顶部居中对齐文本失败,android,android-layout,Android,Android Layout,我曾尝试在topcenter上对齐文本,但失败了 我查过了,但答案不正确 我试过了 <RelativeLayout 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_par

我曾尝试在topcenter上对齐文本,但失败了

我查过了,但答案不正确

我试过了

<RelativeLayout
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"
tools:context=".OnboardingActivity">

<TextView
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="Welcome"
    />

这是我在没有居中的模拟器上运行后得到的结果


只需在
文本视图中使用
android:layout\u centerInParent=“true”

试试这个

<RelativeLayout
    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"
    tools:context=".OnboardingActivity">

<TextView
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Welcome"
    />

</RelativeLayout>

试试这个:

<LinearLayout 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:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Welcome" />

</LinearLayout>

将此添加到您的
RelativeLayout

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Welcome"
        android:layout_centerHorizontal="true"/>


请指定您想要的内容achive@KevinKurien我希望它水平居中,但在top@GEOFFREYMWANGI然后使用
android:layout\u centerInParent=“true”
android:layout\u alignParentTop=“true”
在您的
文本视图中
我希望它位于顶部中间,而不是中间screen@GEOFFREYMWANGI你能分享你的预期产出吗image@GEOFFREYMWANGI而且它还会在屏幕检查中将您的文本视图设置在顶部|中间位置。我已经添加了两个
android:layout\u alignParentTop=“true”
android:layout\u centerInParent=“true”
TextView
中,我看到了我忘了让宽度匹配父对象我不知道谁投了反对票,但这是正确的answer@GEOFFREYMWANGI是的,重力只在线性布局中起作用。。相对布局有自己的标记