2017-06-18 49 views
-2

以下是我为创建简单的聊天应用程序而创建的XML代码。安卓工作室linearLayout难度在放置按钮

<?xml version="1.0" encoding="utf-8"?> 
    <android.widget.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:paddingBottom="16dp" 
    android:paddingEnd="16dp" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingStart="16dp" 
    android:paddingTop="16dp" 
    tools:context="com.example.chatclient1.MainActivity"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:padding="5pt" 
     android:id="@+id/linearLayout" 
     android:background="#ffffff" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" 
     android:layout_margin="0pt"> 

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

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/send" 
      android:id="@+id/sendButton" 

      android:layout_gravity="bottom" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" 
     android:layout_above="@+id/linearLayout" 
     android:padding="5pt" 
     android:background="@null"> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/messageList" 
      android:scrollbars="vertical" 
      android:transcriptMode="alwaysScroll" 
      android:stackFromBottom="true" 
      android:divider="@null" 
      android:background="@null" /> 

    </LinearLayout> 


</android.widget.RelativeLayout> 

实际的问题是,每当我已经多行信息,该发送按钮也喜欢上移:

image

我想解决我的发送按钮。

回答

1

试试这个XML

<?xml version="1.0" encoding="utf-8"?> 
<android.widget.LinearLayout 
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" 
android:paddingBottom="16dp" 
android:paddingEnd="16dp" 
android:paddingLeft="16dp" 
android:paddingRight="16dp" 
android:paddingStart="16dp" 
android:paddingTop="16dp" 
tools:context="com.example.chatclient1.MainActivity"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2" 
    android:padding="5pt" 
    android:background="@null"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/messageList" 
     android:scrollbars="vertical" 
     android:transcriptMode="alwaysScroll" 
     android:stackFromBottom="true" 
     android:divider="@null" 
     android:background="@null" /> 

</LinearLayout> 


<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.3" 
    android:padding="5pt" 
    android:id="@+id/linearLayout" 
    android:background="#ffffff" 
    android:layout_margin="0pt"> 

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

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/send" 
     android:id="@+id/sendButton" 

     android:layout_gravity="bottom" /> 

</LinearLayout> 

onCreate()

getWindow().setSoftInputMode(
      WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 

希望这有助于增加该行MainActivity.java键盘。

+1

工作就像一个魅力!非常感谢你 – HrishikeshKulkarni

+0

@Safa很棒的工作 –