简体   繁体   中英

Android align textview to right

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

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:

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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