简体   繁体   English

如何在RelativeLayout中的TextView右对齐,其layout_width为wrap_content并且其父布局具有wrap_content

[英]How to align right a TextView in RelativeLayout which layout_width is wrap_content and it's parent layout has wrap_content to

I want to make a simple chat layout with the hour of every message aligned bottom right of text (like in Whatsapp). 我想做一个简单的聊天布局,使每条消息的小时数与文本的右下角对齐(例如在Whatsapp中)。 The width of every message changes dynamically so I have to set the layout_width to wrap_content. 每条消息的宽度都会动态变化,因此我必须将layout_width设置为wrap_content。 In this case gravity or layout_gravity doesn't affect the hour but I don't know how to make it different. 在这种情况下,gravity或layout_gravity不会影响小时,但我不知道如何使其与众不同。 Here is the code. 这是代码。 txtMessage is the message and ora is the hour that I want to align bottom right under the message. txtMessage是消息和ORA是我想要的消息对齐右下角小时。 Thank you 谢谢

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/contentWithBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/risponditore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:textSize="10sp"
            android:textStyle="bold"
            android:text=""
            android:visibility="gone"
            android:textColor="@color/bluScuro"/>
        <TextView
            android:id="@+id/txtMessage"
            android:layout_below="@+id/risponditore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:maxWidth="245dp"
            android:textSize="12sp"
            android:layout_gravity="left"/>
        <RelativeLayout
            android:id="@+id/contenitoreOra"
            android:layout_below="@+id/txtMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="right">

            <TextView
                android:id="@+id/ora"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:textColor="@color/grigioScuro"
                android:layout_gravity="bottom|right"/>
            </RelativeLayout>
    </RelativeLayout>
  </LinearLayout>

</RelativeLayout>

EDITED: 编辑:

I want to have this kind of layout: 我想要这种布局:

Text message Text message Text message Text message
Text message Text message Text message Text message
Text message Text message Text message Text message
                                               hour

At the end I obtained a solution using TableLayout with two rows. 最后,我获得了使用带有两行的TableLayout的解决方案。
The first row contain the message and the second row contain the hour. 第一行包含消息,第二行包含小时。
The message is aligned left and the hour right. 消息左对齐,小时右对齐。
Below is the code. 下面是代码。

<?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="wrap_content">

<LinearLayout
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/contentWithBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/risponditore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:textSize="10sp"
            android:textStyle="bold"
            android:text=""
            android:visibility="gone"
            android:textColor="@color/bluScuro"/>

        <TableLayout
            android:id="@+id/tabellaMessaggioOra"
            android:layout_below="@+id/risponditore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TableRow
                android:id="@+id/rigaMessaggio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/txtMessage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:maxWidth="245dp"
                    android:textSize="12sp"
                    android:layout_gravity="left"/>
            </TableRow>
            <TableRow
                android:id="@+id/rigaOra"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/ora"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="10sp"
                    android:textColor="@color/grigioScuro"
                    android:layout_gravity="right"/>

            </TableRow>
        </TableLayout>

    </RelativeLayout>
</LinearLayout>
</RelativeLayout>  

You can use android:layout_toRightOf Logic . 您可以使用android:layout_toRightOf Logic

Positions the left edge of this view to the right of the given anchor view ID. 将此视图的左边缘定位在给定的锚视图ID的右侧。 Accommodates left margin of this view and right margin of anchor view. 容纳该视图的左边缘和锚视图的右边缘。

Example

<Button
        android:id="@+id/btnButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Intellij"/>

    <Button
        android:id="@+id/btnButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Amiyo"
        android:layout_toRightOf="@+id/btnButton1"/>

FYQ FYQ

Use android:gravity="bottom|right" 使用android:gravity="bottom|right"

<TextView
                android:id="@+id/ora"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:text="Text message"
                android:gravity="bottom|right"/>

You may want to use LinearLayout in this case, it will work if you give gravity in LinearLayout. 在这种情况下,您可能需要使用LinearLayout,如果在LinearLayout中赋予重力,它将可以使用。 Ignore this if you want RelativeLayout strictly. 如果要严格使用RelativeLayout,请忽略此内容。

<LinearLayout
            android:id="@+id/contenitoreOra"
            android:layout_below="@+id/txtMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="right">

            <TextView
                android:id="@+id/ora"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:textColor="@color/grigioScuro"
                android:layout_gravity="bottom|right"/>
 </LinearLayout>

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

相关问题 右对齐和居中对齐TextView,而无需layout_width =“ wrap_content” - Right align and center align TextView without layout_width=“wrap_content” android:layout_width =“ wrap_content”在RelativeLayout中不起作用 - android:layout_width=“wrap_content” does not work in a RelativeLayout 使用layout_width =“ wrap_content”获取TextView的可用宽度 - Get available width for TextView with layout_width=“wrap_content” 如何以编程方式将 layout_width 设置为 WRAP_CONTENT 到 ConstraintLayout - How to set layout_width as WRAP_CONTENT to ConstraintLayout programmatically 当layout_width为wrap_content时,如何获取自定义视图的宽度? - How to get custom View's width when layout_width is wrap_content? 将ImageView与设置为wrap_content的父布局向右对齐 - Align ImageView to the right with parent layout set to wrap_content Android Toast消息是否被强制换行为wrap_content layout_width? - are android toast messages forced to wrap_content layout_width? TextView在breakStrategy =“balanced”和layout_width =“wrap_content”时出错 - TextView works wrong with breakStrategy=“balanced” and layout_width=“wrap_content” 如何使用android:layout_width =“ wrap_content”获取以像素为单位的宽度? - how to get width in pixels of view with android:layout_width=“wrap_content”? android:相对布局宽度wrap_content - android : Relative layout width wrap_content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM