RelativeLayout中元素的排列
Arrangement of elements in 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/AndroidRuntime: FATAL EXCEPTION: main
Process: asus.example.com.oop, PID: 5020
java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
怎么了?登录在密码上方,密码在按钮上方。我不明白为什么会有循环依赖
解决方案
与其使用 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>
尝试一下,希望对您有所帮助。
有一个 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 Process: asus.example.com.oop, PID: 5020 java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
怎么了?登录在密码上方,密码在按钮上方。我不明白为什么会有循环依赖
解决方案
与其使用 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>
尝试一下,希望对您有所帮助。