Android 将 textview 向右对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21808808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:07:13  来源:igfitidea点击:

Android align textview to right

androidxmltextviewalignment

提问by Vesica Piscis

in Android XML I am try to set the alignment of the text to right:

在 Android XML 中,我尝试将文本的对齐方式设置为右侧:

android:gravity="center_vertical|right"

I also align it in the center of the parent, hence the center add in. Now I have the text on a right alignment but I want to assign it to the position of the parents left:

我也将它对齐在父母的中心,因此中心添加。现在我有一个右对齐的文本,但我想将它分配给左父母的位置:

android:layout_alignParentLeft="true"

But this only keeps the alignment of the text and does not align to the parents center? Heres the entire view xml code:

但这只是保持文本的对齐,而不是与父母中心对齐?下面是整个视图 xml 代码:

<TextView
    android:id="@+id/textview_example"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|right"
    android:layout_alignParentLeft="true"
    android:textColor="#474747"
    android:textSize="14sp" />

Can someone help me create a solution for having the textview's text aligned to the right but be assigned to the parents left?

有人可以帮我创建一个解决方案,让 textview 的文本向右对齐但分配给左边的父母吗?

回答by keshav

Actually ur code is working but it will be clear if you give width less than parent, try this change

实际上你的代码正在工作,但如果你给的宽度小于父级就会很清楚,试试这个改变

<TextView
        android:id="@+id/textview_example"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|right"
        android:textColor="#474747"
        android:text="ABAGSADGDBAJDHADNA"
        android:textSize="14sp" />

回答by Silambarasan Poonguti

Try this....

尝试这个....

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#E6F2B2"
            android:gravity="center"
            android:padding="3dp" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="end|center"
                android:text="@string/title"
                android:textColor="#323232"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>

Happy coding......

快乐编码......