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

时间:2018-12-19 07:12:38

标签: android android-layout

我尝试在顶部中心对齐文本,但失败

我已检查This question,但答案失败

我尝试过

<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"
    />

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

enter image description here

3 个答案:

答案 0 :(得分:1)

将此添加到您的RelativeLayout

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

答案 1 :(得分:0)

只需在您的 android:layout_centerInParent="true"

中使用 TextView

尝试一下

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

答案 2 :(得分:0)

尝试一下:

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