[英]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.