在RelativeLayout中防止视图重叠

Gnu*_*bio 13 android android-layout

我正在尝试开发一个Android应用程序,但我在GUI设计方面遇到了一些问题.屏幕截图的以下部分是我的问题(红色和蓝色线是由Android调试选项生成的).

我的问题

这是代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/caption"
    android:layout_below="@+id/caption" >

    <ImageButton
        android:id="@+id/button_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/content_edit"
        style="?android:attr/borderlessButtonStyle" />

    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="@string/num_placeholder"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,TextView myText与ImageButton button_edit重叠.一个简单的解决方案是使用LinearLayout而不是RelativeLayout.问题是:

  1. 我需要右边的编辑按钮
  2. 我需要文本来填充布局的其余部分(显然在编辑按钮的左侧)

我还尝试将myText的layout_width设置为'fill_parent',但它会填充编辑按钮左侧的整个屏幕的其余部分.

预期的行为是myText增加其高度(成为两行TextView)而不是重叠'button_edit'

谁能帮我?谢谢

moh*_*hni 19

在TextView布局中使用layout_toLeftOf

像这样:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/caption"
android:layout_below="@+id/caption" >

<ImageButton
    android:id="@+id/button_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/content_edit"
    style="?android:attr/borderlessButtonStyle" />

<TextView
    android:id="@+id/myText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="@string/num_placeholder"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_toLeftOf="@+id/button_edit" />
Run Code Online (Sandbox Code Playgroud)