相对布局覆盖在相对布局组件中声明的组件

发布于 2025-01-05 06:45:11 字数 1212 浏览 3 评论 0原文

作为一名 Android 开发人员,我是个新手,目前我正在了解relativelayout,但面临一个较小的问题,即 Textview 重叠在图像上。在这里,我向您提供我想要的图像和我得到的图像,以及我所做的很多代码。所以请帮助我。

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/firstImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/firstImage"
        android:layout_alignBaseline="@id/firstImage"
        android:text="@string/menu" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />
</RelativeLayout>

这是我想要的布局图像。

在此处输入图像描述

请帮助我。

提前致谢..

I am very new in as a android developer and i am currently understanding the RelativeLayout but facing a smaller problem that Textview is overLapping on the Image. Here I am providing you the Image that what i want and what i am getting and also the code that much i have did. so please Help me.

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/firstImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/firstImage"
        android:layout_alignBaseline="@id/firstImage"
        android:text="@string/menu" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />
</RelativeLayout>

Here is the Image that i want in layout.

enter image description here

Please Help me.

Thanks in Advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰魂雪魄 2025-01-12 06:45:11

使用这些属性

 android:layout_centerHorizontal="true"    
 android:layout_centerVertical="true"

进行文本视图..

检查此代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/relativeLayout1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/firstImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/logo2"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/logo2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"      
    android:layout_centerHorizontal="true"    
    android:layout_centerVertical="true"     
    android:text="menu" />

use these property

 android:layout_centerHorizontal="true"    
 android:layout_centerVertical="true"

for text view..

check this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/relativeLayout1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/firstImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/logo2"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/logo2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"      
    android:layout_centerHorizontal="true"    
    android:layout_centerVertical="true"     
    android:text="menu" />

陈年往事 2025-01-12 06:45:11

将您的 xml 文件代码替换为以下内容...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/firstImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />

    <ImageView
        android:id="@+id/firstImage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/firstImage2"
        android:layout_toRightOf="@+id/firstImage"
        android:text="@string/menu" />

</RelativeLayout>

replace your xml file code with this...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/firstImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />

    <ImageView
        android:id="@+id/firstImage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/splash1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/firstImage2"
        android:layout_toRightOf="@+id/firstImage"
        android:text="@string/menu" />

</RelativeLayout>
吃→可爱长大的 2025-01-12 06:45:11

您可以切换到图形模式并修复上述问题。这并不是说您必须直接在 XML 中定义所有内容,但您可以切换到图形模式并修复此问题。

You can switch to graphic mode and fix the above. It is not that you have to define every thing in XML directly but you can switch to graphic mode and fix this.

伤痕我心 2025-01-12 06:45:11

添加两个 TextView 属性 android:layout_centerHorizo​​ntal="true"android:gravity="center",这样文本就会在 TextView 中居中。

如果文本太大(它会与图像重叠),则可能会出现问题。为了避免这种情况,您应该像这样定义布局:

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
  <ImageView
    android:id="@+id/firstImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/splash1"
  />
  <ImageView
    android:id="@+id/secondImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/splash1" 
  />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignTop="@id/firstImage"
    android:layout_alignBottom="@id/firstImage"
    android:layout_toRightOf="@id/firstImage"
    android:layout_toLeftOf="@id/secondImage"
    android:gravity="center"
    android:text="@string/menu"
  />
</RelativeLayout>

注意,两个图像视图是如何在 TextView 之前定义的。这是因为这些图像视图将成为您的锚视图,这意味着其他所有内容都将根据它们进行对齐。您的 TextView 现在位于 2 个 ImageView 之间,因此无论有什么文本,它都不会重叠任何内容。

Add two more properities of textview android:layout_centerHorizontal="true" and android:gravity="center", so text will be centered inside yout TextView.

In this there might be a problem if text is too big (it will overlap the images). To avoid that you should define your layout like that:

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
  <ImageView
    android:id="@+id/firstImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/splash1"
  />
  <ImageView
    android:id="@+id/secondImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/splash1" 
  />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignTop="@id/firstImage"
    android:layout_alignBottom="@id/firstImage"
    android:layout_toRightOf="@id/firstImage"
    android:layout_toLeftOf="@id/secondImage"
    android:gravity="center"
    android:text="@string/menu"
  />
</RelativeLayout>

Notice, how both image views are defined before TextView. That's because those image views will be your anchor views, meaning everything else will be aligned depending on them. Your TextView is now between 2 ImageViews so no matter what text there is, it will not overlap anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文