2017-01-29 61 views
-2

我正在创建一个原生的android应用程序,我正在尝试使textview(可点击的按钮)相对于文本,所以如果有很多文本,“按钮”将移动到下一行。如果有空间,我想对齐文本旁边的“按钮”。我正在考虑使用相对布局,但不知道要修改哪些属性。如果有人能分享他/她的专业知识,那将是非常棒的。将TextView对齐到另一个TextView的右边,并跳到下一行?

Example of Alignment

这个问题是非常相似,这post,但它是不同的,因为我想要的按钮也为同一行,如果有空间(第三个选项)。

+0

我不认为你可以让你的第三个展示的文字+按钮。包装的TextView制作一个矩形,所以没有“间隙”来放置一个按钮 –

回答

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

相关问题