繁体   English   中英

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>

谢谢

<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 的中心,是 30dp marginLeft

在此处输入图片说明

你可以试试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="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"
/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM