在RelativeLayout中的EditText粘在键盘顶部

时间:2016-06-30 06:53:50

标签: android android-edittext android-softkeyboard android-relativelayout

我希望我的包含我的EditText和我的ImageButton的RelativeLayout在键盘顶部始终保持粘性和全宽。

显示对话框时,键盘始终显示。

当您提交评论时,它应该与YouTube应用行为相似。

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">

<EditText
    android:id="@+id/comment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Add anonymous comment"
    android:textAppearance="@style/CodeFont" />

<ImageButton
    android:id="@+id/submit_comment"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/send_red" />

</RelativeLayout>

DialogComment dialogComment = new DialogComment(mainActivity, WPPostDataView.this);

            dialogComment.show();

            //Grab the window of the dialog, and change the width
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

            Window window = dialogComment.getWindow();
            lp.copyFrom(window.getAttributes());
            //This makes the dialog take up the full width
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // request keyboard
    this.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

1 个答案:

答案 0 :(得分:0)

<?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:layout_w`enter code here`idth="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activity.ChatActivity"
    tools:showIn="@layout/activity_chat">

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="50dp"
        android:scrollbars="vertical" />

<LinearLayout
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:weightSum="4">

<EditText android:id="@+id/message"
            android:layout_width="0dp"
            android:hint="Enter message"
            android:paddingLeft="10dp"
            android:background="@null"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="16dp"
            android:lines="1"
            android:layout_height="wrap_content"
            android:layout_weight="3" />

<Button android:id="@+id/btn_send"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="SEND"
            android:textSize="16dp"
            android:textColor="@color/colorPrimary" />

</LinearLayout>

</RelativeLayout>