Android 相对论中元素的排列

Android 相对论中元素的排列,android,android-layout,android-relativelayout,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" androi

有一个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/AndroidRuntime:致命异常:主 流程:asus.example.com.oop,PID:5020 java.lang.IllegalStateException:RelativeLayout中不能存在循环依赖项


怎么了?登录在密码上方,密码在按钮上方。我不明白为什么会有循环依赖

解决方案

与其在上面使用
布局,不如在下面使用
布局:

像这样:

<?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>


试试看,希望能有所帮助。

这意味着你将视图从左到右和从右到左对齐,因此删除依赖项对齐。你能尝试一个干净的构建吗。我看不到循环依赖关系是的,它是有效的,但区别是什么?为什么上面的布局_不起作用?@SergeiMikhailovskii您的代码在我的系统中没有错误。对于您,请尝试在您的按钮中添加此行,然后选中