Расположение элементов в RelativeLayout - PullRequest
0 голосов
/ 04 октября 2018

Существует 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: FATAL EXCEPTION: main Процесс: asus.example.com.oop, PID: 5020 java.lang.IllegalStateException: циклические зависимости не могут существовать в RelativeLayout

В чем дело?Логин над паролем, пароль над кнопкой.Я не понимаю, почему существует круговая зависимость

1 Ответ

0 голосов
/ 04 октября 2018

Решение

Вместо 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>

Попробуй, надеюсь, это поможет.

...