简体   繁体   English

如何在右角对齐文本视图?

[英]How to align textview in right corner?

I have the following xml .我有以下 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 .我想将 id unread_count 的 textview 移动到 parent 的右侧。 How can I do this ?我怎样才能做到这一点 ?

If I set android:layout_alignParentRight="true" then I have the following picture :如果我设置 android:layout_alignParentRight="true" 那么我有以下图片:

在此处输入图片说明

I simply want to get the following picture :我只想得到以下图片:

在此处输入图片说明

add this in your textview of id unread_count在您添加此textview 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添加一些右边距来调整textview位置

Just set android:layout_alignParentRight="true"只需设置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 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"    
           />

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

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