简体   繁体   中英

How to align textview in right corner?

I have the following xml .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/smsItemContainerRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_gravity="center_horizontal|top"
            android:text="SMS Inbox"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/unread_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="-20dp"
            android:layout_toRightOf="@+id/textView"
            android:background="@drawable/notification_bg"
            android:gravity="center"
            android:text="88"
            android:textColor="@android:color/background_light"
            android:textSize="20sp"
            android:textStyle="bold"    
            android:visibility="visible" />
    </RelativeLayout>

    <ListView
        android:id="@+id/SMSList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />


</LinearLayout>

For this , I have the following layout :

在此处输入图片说明

I want to shift textview of id unread_count to the right of parent . How can I do this ?

If I set android:layout_alignParentRight="true" then I have the following picture :

在此处输入图片说明

I simply want to get the following picture :

在此处输入图片说明

add this in your textview of id unread_count

 android:layout_alignParentRight="true"

and remove this

android:layout_toRightOf="@+id/textView"

don't give negative margin

add some right margin to adjust position of your textview

Just set android:layout_alignParentRight="true"

and try with this

android:gravity="right"
android:textAlignment="gravity"

https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignParentRight

Remove the following lines:

android:layout_marginLeft="-20dp"  android:layout_toRightOf="@+id/textView"

and include this line

android:layout_alignParentRight="true"

Just Remove this Line

  android:layout_toRightOf="@+id/textView"

and add this Line

  android:layout_alignParentRight="true"
 <TextView
            android:id="@+id/unread_count"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/textView"
            android:background="@drawable/notification_bg"
            android:gravity="center"
            android:text="88"
            android:layout_alignBottom="@+id/textView"
            android:textColor="@android:color/background_light"
            android:textSize="16sp"
            android:textStyle="bold"    
           />

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