首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Android RelativeLayout叠加的元素

使用Android RelativeLayout叠加的元素
EN

Stack Overflow用户
提问于 2014-10-16 12:02:22
回答 3查看 1.9K关注 0票数 0

我有下面的Android布局

代码语言:javascript
复制
<?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:paddingBottom="16dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/slideTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:padding="@dimen/px20"
        android:text="@string/login_message"
        android:textSize="@dimen/px25"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/slideDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/password"
        android:drawablePadding="@dimen/px20"
        android:drawableStart="@drawable/password"
        android:padding="@dimen/px20"
        android:text="@string/login_message_body"
        android:textSize="@dimen/px20" />

    <TextView
        android:id="@+id/swipeMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="" />

</RelativeLayout>

但是所有的元素都位于屏幕的顶部,一个在另一个上面,就像它们没有占据任何空间一样。

文档中,所有元素都垂直地放置在另一个元素下面,这似乎不是这样的。

这里发生了什么事?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-10-16 12:10:50

因此,您需要使用其他组件的id,然后正确地对齐。

例如,带有id @+id/slideDescription@+id/slideDescription也应该将android:layout_below="@+id/slideTitle"作为xml的属性之一。

带有id @+id/swipeMessage@+id/swipeMessage也应该将android:layout_below="@+id/slideDescription"作为xml的属性之一。

票数 2
EN

Stack Overflow用户

发布于 2014-10-16 12:06:20

为了将一个视图放置在RelativeLayout中的另一个视图下面,您必须使用下面属性并设置您希望位于指定视图之上的视图ID。但实际上,为了将视图垂直放置在彼此下面,使用方向设置为垂直的LinearLayout更方便。

票数 2
EN

Stack Overflow用户

发布于 2014-10-16 12:20:00

layout_below在上面的xml代码中被遗漏了,我用它替换了代码,请使用它。

在相对布局中,元素网将相对于其他元素排列,为了做到这一点,我们应该使用各个视图单元的id值。

android:layout_below="@id/slideTitle“应该放在描述文本视图中,android:layout_below=”@id/幻灯片描述“应该放在消息文本视图

为了获得您想要的输出,请使用以下代码

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp" >

<TextView
    android:id="@+id/slideTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dip"
    android:padding="@dimen/px20"
    android:text="@string/login_message"
    android:textSize="@dimen/px25"
    android:textStyle="bold" />

<TextView
    android:id="@+id/slideDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/slideTitle"
    android:drawableLeft="@drawable/password"
    android:drawablePadding="@dimen/px20"
    android:drawableStart="@drawable/password"
    android:padding="@dimen/px20"
    android:text="@string/login_message_body"
    android:textSize="@dimen/px20" />

<TextView
    android:id="@+id/swipeMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/slideDescription"
    android:text="" />

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26403893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档