繁体   English   中英

如何修复线性布局底部的按钮:Android Studio

[英]How to fix the button at the bottom on my Linear Layout: Android Studio

我的UI上有一个ImagePickerButtonEditTextSend Button,如下所示:

在此处输入图片说明

问题出在“ Send按钮上,该按钮未固定在“线性布局”的底部。 当我输入多行文本时,谁能说出如何将按钮的位置固定在底部

这是我的布局代码:

<?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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxWidth="@android:dimen/notification_large_icon_height"
    tools:context="com.pc.wecare.MainActivity">

    <ListView
        android:id="@+id/messageListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linearLayout"
        android:divider="@color/cardview_dark_background"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/item_message" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/photoPickerButton"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_gravity="bottom"
            android:background="@android:drawable/ic_menu_gallery" />

        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_gravity="bottom"
            android:enabled="false"
            android:text="SEND" />

    </LinearLayout>

</RelativeLayout>

尝试使用

android:baselineAligned="false"

在您的LinearLayout 请让我知道这对你有没有用。 这就是我解决类似问题的方式,但是其中没有包含editText

您可以将按钮包装在另一个线性布局中,以实现此目的。

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:enabled="false"
        android:text="SEND"
        />

    </LinearLayout>

只需将android:baselineAligned="false"属性添加到LinearLayout

如下更新您的XML:

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:orientation="horizontal"
    android:baselineAligned="false">

    .................
    .........................

</LinearLayout>

这是使用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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxWidth="@android:dimen/notification_large_icon_height">

    <ListView
        android:id="@+id/messageListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linearLayout"
        android:divider="@color/cardview_dark_background"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/item_message" />

    <RelativeLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/photoPickerButton"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@android:drawable/ic_menu_gallery" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:enabled="false"
            android:text="SEND" />

        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/photoPickerButton"
            android:layout_toLeftOf="@id/sendButton"
            android:layout_weight="1"/>

    </RelativeLayout>

</RelativeLayout>

输出:

在此处输入图片说明

希望这会有所帮助〜

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM