"layout_toEndOf" 定位不正确
"layout_toEndOf" not positioning correctly
我想在 Button
左边显示一个 EditText
。因此,我定义了以下 .xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
tools:context="de.abelssoft.lowbatterywarner.DataActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/data_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
app:layout_anchor="@id/coordinator_layout"
app:layout_anchorGravity="bottom"
android:background="@color/light_grey">
<EditText android:id="@+id/edit_message"
android:background="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="4"
android:hint="Nachricht schreiben..." />
<Button
android:id="@+id/button_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/button_folder"
android:background="@drawable/folder"
android:onClick="onFolderClick"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
虽然我使用的是 layout_toEndOf
,但按钮位于屏幕中央。为什么会这样?这与相对布局是 CoordinatorLayout 的子布局这一事实有关吗?
将您的 RelativeLayout
更改为:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
app:layout_anchor="@id/coordinator_layout"
app:layout_anchorGravity="bottom"
android:background="@color/light_grey">
<EditText android:id="@+id/edit_message"
android:background="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="4"
android:hint="Nachricht schreiben..." />
<Button
android:id="@+id/button_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/edit_message"
android:background="@drawable/folder"
android:onClick="onFolderClick"/>
</RelativeLayout>
您在按钮主体中使用 layout_toEndOf
作为同一按钮的结尾:
<Button
android:id="@+id/button_folder"
android:layout_toEndOf="@id/button_folder"
/>
要么改变它,要么做这两个步骤
1- 将您的按钮右对齐:
android:layout_alignParentRight="true"
2- 将您的 editText 设置在它的左侧:
android:layout_toLeftOf="@+id/button_folder"
您给按钮的 ID 有误....只需复制下面的代码即可
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
tools:context="de.abelssoft.lowbatterywarner.DataActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/data_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/light_grey"
app:layout_anchor="@id/coordinator_layout"
app:layout_anchorGravity="bottom">
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:hint="Nachricht schreiben..."
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="4" />
<Button
android:id="@+id/button_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/edit_message"
android:background="@drawable/folder"
android:onClick="onFolderClick" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
我想在 Button
左边显示一个 EditText
。因此,我定义了以下 .xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
tools:context="de.abelssoft.lowbatterywarner.DataActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/data_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
app:layout_anchor="@id/coordinator_layout"
app:layout_anchorGravity="bottom"
android:background="@color/light_grey">
<EditText android:id="@+id/edit_message"
android:background="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="4"
android:hint="Nachricht schreiben..." />
<Button
android:id="@+id/button_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/button_folder"
android:background="@drawable/folder"
android:onClick="onFolderClick"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
虽然我使用的是 layout_toEndOf
,但按钮位于屏幕中央。为什么会这样?这与相对布局是 CoordinatorLayout 的子布局这一事实有关吗?
将您的 RelativeLayout
更改为:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
app:layout_anchor="@id/coordinator_layout"
app:layout_anchorGravity="bottom"
android:background="@color/light_grey">
<EditText android:id="@+id/edit_message"
android:background="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="4"
android:hint="Nachricht schreiben..." />
<Button
android:id="@+id/button_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/edit_message"
android:background="@drawable/folder"
android:onClick="onFolderClick"/>
</RelativeLayout>
您在按钮主体中使用 layout_toEndOf
作为同一按钮的结尾:
<Button
android:id="@+id/button_folder"
android:layout_toEndOf="@id/button_folder"
/>
要么改变它,要么做这两个步骤
1- 将您的按钮右对齐:
android:layout_alignParentRight="true"
2- 将您的 editText 设置在它的左侧:
android:layout_toLeftOf="@+id/button_folder"
您给按钮的 ID 有误....只需复制下面的代码即可
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
tools:context="de.abelssoft.lowbatterywarner.DataActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/data_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/light_grey"
app:layout_anchor="@id/coordinator_layout"
app:layout_anchorGravity="bottom">
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:hint="Nachricht schreiben..."
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="4" />
<Button
android:id="@+id/button_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/edit_message"
android:background="@drawable/folder"
android:onClick="onFolderClick" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>