【问题标题】:Elements overlapping on top of each other in relativeLayout元素在 relativeLayout 中相互重叠
【发布时间】:2014-07-15 01:23:29
【问题描述】:

我创建了一个 Activity 并使用了 RelativeLayout,它默认出现在 Android Studio 中。

我试图在我的 TextView 之前放置一个按钮,但我无法做到。

结果如下:

这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <TextView
        android:id="@+id/heading"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/button"
        android:text="@string/sachin_jain"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignLeft="@+id/heading"
        android:layout_alignTop="@+id/heading"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>

看起来我错过了什么。任何帮助将不胜感激!

【问题讨论】:

标签: android android-layout textview android-relativelayout


【解决方案1】:

我发现的解决方案是:使用android:layout_below 而不是android:layout_alignTop / android:layout_alignRight

<TextView
    android:id="@+id/heading"
    android:text="My First Activity with Relative Layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    />

<Button
    android:id="@+id/button"
    android:text="Save"
    android:layout_width="300px"
    android:layout_height="wrap_content"
    android:layout_below="@id/heading"
    android:layout_margin="20px"
    android:layout_centerHorizontal="true"
    />

截图如下:

【讨论】:

    【解决方案2】:

    我猜你首先想要一个按钮,然后是一个 textview。然后你可以这样做:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">
    
    <TextView
        android:id="@+id/heading"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    
    <Button
        android:id="@+id/button"
        android:text="@string/sachin_jain"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignRight="@+id/heading"
        android:layout_alignTop="@+id/heading"
        android:layout_centerHorizontal="true"
        />
    
    </RelativeLayout>
    

    你的问题是你有:

     android:layout_alignLeft="@+id/heading"
        android:layout_alignTop="@+id/heading"
    

    在使两个视图相互对齐的按钮中。

    【讨论】:

    • ..您的解决方案也无法正常工作。 layout_alignRight 和 'layout_alignTop` 将按钮的顶部和右侧与文本视图对齐,因此它们将再次相互重叠
    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多