2017-04-22 318 views
0

我有一个ImagePickerButton,如下图所示EditTextSend按钮在我的UI:如何在我的线性布局底部的固定按钮:Android Studio中

enter image description here

的问题是与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> 

回答

2

尝试使用

android:baselineAligned="false" 

在你LinearLayout。请让我知道这对你有没有用。这就是我解决类似问题的方法,但它不包括editText

1

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

<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> 
1

只需添加属性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> 

OUTPUT:

enter image description here

希望这会有所帮助〜

相关问题