RelativeLayout中元素的排列

时间:2018-10-04 15:46:07

标签: android android-layout android-relativelayout

有一个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/password"
        android:layout_centerHorizontal="true"
        android:hint="@string/loginText"
        android:layout_marginBottom="20dp"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button"
        android:hint="@string/passwordText"
        android:layout_marginBottom="20dp"
        android:inputType="textPassword"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

运行程序时,出现以下错误:

  

E / Android运行时:致命异常:主要       程序:asus.example.com.oop,PID:5020       java.lang.IllegalStateException:RelativeLayout中不能存在循环依赖项

怎么了?登录名高于密码,密码高于按钮。我不明白为什么存在循环依赖

1 个答案:

答案 0 :(得分:1)

解决方案

使用layout_above会是一个不错的选择,而不是使用layout_below

赞:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="20dp"
        android:hint="Hello"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login"
        android:hint="Password"
        android:text="Hello"
        android:layout_marginBottom="20dp"
        android:inputType="textPassword"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:layout_below="@+id/password"/>

</RelativeLayout>

尝试一下,希望对您有所帮助。