" layout_toEndOf"没有正确定位

时间:2016-02-22 12:16:05

标签: android xml android-layout position

我想在EditText左侧显示Button。因此,我定义了以下.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的孩子吗?

3 个答案:

答案 0 :(得分:0)

将您的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>

答案 1 :(得分:0)

您在按钮正文中使用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" 

答案 2 :(得分:0)

你给错误的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>