TextView 居中对齐 - Android

TextView align center - Android

我有这样的文本视图,

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

它水平居中对齐,但现在我想将它从中心向左移动 20p。所以我尝试了 android:layout_marginStart="30dp" android:layout_marginLeft="30dp"

但是它一直在中间,我怎么能把它移到左边,但有中心的参考?

此处已满xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.spencer.one.SplashScreenActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginStart="30dp"
    android:layout_marginLeft="30dp"/>
</RelativeLayout>

谢谢

你可以试试android:paddingRight="30dp",像这样。

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:gravity="center"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

你可以试试这个代码

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:gravity="left"
    />

试试这个:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_marginRight="20dp"/>

您试图将它向左移动,因此在右侧留出 20dp 的边距会将其向左推 20dp,并且仍保持居中作为参考。

有点棘手但有效。

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="whatever"
        />
    <View
        android:layout_width="20dp"
        android:layout_height="match_parent"
        />
</LinearLayout>

首先创建linearlayout,将textview放在里面。并添加空视图以提供填充。

就这些了。

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_marginLeft="30dp"
        android:gravity="center"/>

内容在 textView 内居中且 marginLeft 为 30dp

你应该使用 padding 而不是 margin ...

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_centerHorizontal="true"
    android:paddingRight="30dp"
/>