【问题标题】:Android: How to fix the image that is overlapping my text?Android:如何修复与我的文本重叠的图像?
【发布时间】:2023-03-17 22:45:02
【问题描述】:

如何解决此错误。基本上,图像与文本重叠。 我们可以在 XML 中解决这个问题吗?

这是代码(当然,它不完整)。

<?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="match_parent"
    android:background="#FFCC0000"
    >

    <TextView
        android:background="#55000000"
        android:id="@+id/centerButton"
        android:text="RELATIVE LAYOUT RULES!"
        android:textSize="33dp"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_margin="40dp"
        android:padding="20dp"
        android:layout_below="@+id/centerButton"
        android:layout_centerInParent="true"
        android:text="PUSH ME!"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4" />

    <ImageView
        android:scaleType="centerInside"
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/Cat"
        android:layout_above="@+id/centerButton"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="39dp" />


</RelativeLayout>

错误截图:

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/19065232/…
  • @Xedon 你能分享@drawable/Cat image
  • 看起来你的 textview 在 imageview 后面。尝试将 textview 代码放在 xml 中的 imageview 下面。这会将文本视图带到图像的前面
  • 我试过你分享的布局。有用。不是你的预览需要刷新吗?在实际设备上如何显示?

标签: android xml


【解决方案1】:

将TextView->ImageView的顺序改为ImageView->TextView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFCC0000"
    >

    <ImageView
        android:scaleType="centerInside"
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/Cat"
        android:layout_above="@+id/centerButton"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="39dp" />


    <TextView
        android:background="#55000000"
        android:id="@+id/centerButton"
        android:text="RELATIVE LAYOUT RULES!"
        android:textSize="33dp"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <Button
        android:layout_margin="40dp"
        android:padding="20dp"
        android:layout_below="@+id/centerButton"
        android:layout_centerInParent="true"
        android:text="PUSH ME!"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4" />



</RelativeLayout>

【讨论】:

    【解决方案2】:

    不确定您到底在寻找什么,但我可以想到两个快速解决方案:

    首先,您需要将TextViewid 标签更改为其他标签,它目前与您的Button 具有相同的id,这不好。我想你的意思是centerText

    1.您希望TextView 出现在ImageView

    要实现这一点,您实际上只需将TextView XML 元素移动到ImageView 元素下方。因为视图在 XML 布局方面是按从上到下的顺序“分层”的,所以这会将 TextView 放在 ImageView 的顶部。

    2。您希望 ImageView 不与 TextView 重叠

    您可以将android:layout_above="@+id/centerButton" 更改为android:layout_above="@+id/centerText",其中centerText 是您的TextView,当然带有更正的id 标签。

    希望能解决您的问题!如果没有,请更具体地说明您想要实现的目标!

    【讨论】:

      【解决方案3】:

      Android 在 XML 文件中从上到下绘制布局。如果你想让你的 TextView 画在上面,你需要把它放到底部。

      【讨论】:

        猜你喜欢
        • 2015-07-07
        • 2016-09-11
        • 1970-01-01
        • 1970-01-01
        • 2019-08-16
        • 1970-01-01
        • 2011-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多