android – 防止在RelativeLayout中重叠的视图

我正在尝试开发一个 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>

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

>我需要右边的编辑按钮
>我需要文本填写剩下的布局(在编辑按钮的左边显然)

我也试图将myText的layout_width设置为“fill_parent”,但它会填充编辑按钮左侧的屏幕的其余部分.

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

有人可以帮我吗谢谢

解决方法

在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" />

相关文章

ADB Remote ATV Android TV 的遥控器,基于 ADB Shell 命令 ...
使用Flutter自带的SearchDelegate组件实现搜索界面,通过魔改...
上篇文章讲解了怎么使用Kotlin的协程配合Retrofit发起网络请...
安卓开发——WebView+Recyclerview文章详情页,解决高度...
Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...