将TextView对齐另一个TextView的右侧并中断到下一行?

时间:2017-01-29 05:34:42

标签: android xml layout

我正在创建一个原生的Android应用程序,我正在尝试相对于文本制作textview(可点击按钮),所以如果有很多文本,那么"按钮"将移至下一行。如果有空间,我想对齐"按钮"在文本旁边。我正在考虑使用相对布局,但并不确切知道要修改哪些属性。如果有人可以分享他/她的专业知识,那将是很棒的。

Example of Alignment

这个问题与此post非常相似,但它有所不同,因为如果有空格(第三个选项),我希望按钮也是同一行。

1 个答案:

答案 0 :(得分:1)

使用RelativeLayout是正确的,请尝试以下

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/btn"
        android:text="Hello World!asdfasdfasfasdfdasfsadfsfsafasfasfasdasdasdaas" />

    <Button
        android:id="@id/btn"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:layout_centerVertical="true"/>
</RelativeLayout>

结果: enter image description here

相关问题