当键盘出现在屏幕

时间:2017-07-22 21:58:04

标签: android xml android-layout

我花了一整天时间研究如何实现这一目标。我有一个scrollview,它下面的edittext和edittext右边的按钮。

1)我试图向上移动所有这些内容,因为键盘出现时没有任何视图重叠。

This is what I have and this is what happens when edittext is focused. There is this small layer that overlaps my scrollview content

I want to achieve this whatsapp-like content where all content moves upwards but edittext and button do not overlap content above

我尝试了不同的解决方案,如margin,padding,windowsoftinputMode,以不同的方式更改我的布局,但我仍然无法实现这一点。请帮我!这是我的代码:

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

<android.support.v7.widget.Toolbar
    android:id="@+id/detailsToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorToolbar"
    android:elevation="4dp"
    android:theme="@style/Toolbar_iconTheme" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/detailsToolbar"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/displayPicture"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxHeight="100dp"
                    android:maxWidth="80dp"
                    android:minHeight="80dp"
                    android:minWidth="70dp"
                    android:padding="4dp"
                    android:src="@mipmap/ic_launcher_round" />

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_toRightOf="@+id/displayPicture"
                    android:paddingLeft="4dp"
                    android:paddingTop="4dp"
                    android:text="ProfileName"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                    android:textColorHighlight="@android:color/holo_blue_dark"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/post"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/name"
                    android:layout_toRightOf="@+id/displayPicture"
                    android:padding="4dp" />

                <TextView
                    android:id="@+id/commentsline"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/displayPicture"
                    android:background="@android:color/darker_gray"
                    android:padding="4dp"
                    android:text="comments"
                    android:textAlignment="center" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/commentsListView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/commentsline" />

            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/messageText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="32dp"
            android:background="@drawable/rounded_corners"
            android:scrollbars="vertical" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@android:drawable/ic_menu_send" />

    </RelativeLayout>
</LinearLayout>

和Manifest:

<activity
        android:name=".DetailsActivity"
        android:windowSoftInputMode="adjustResize"/>

1 个答案:

答案 0 :(得分:0)

使你的布局像这样

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/detailsToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/Toolbar_iconTheme" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/displayPicture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxHeight="100dp"
                android:maxWidth="80dp"
                android:minHeight="80dp"
                android:minWidth="70dp"
                android:padding="4dp"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_toRightOf="@+id/displayPicture"
                android:paddingLeft="4dp"
                android:paddingTop="4dp"
                android:text="ProfileName"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:textColorHighlight="@android:color/holo_blue_dark"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/post"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:layout_toRightOf="@+id/displayPicture"
                android:padding="4dp" />

            <TextView
                android:id="@+id/commentsline"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/displayPicture"
                android:background="@android:color/darker_gray"
                android:padding="4dp"
                android:text="comments"
                android:textAlignment="center" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/commentsListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/commentsline" />

        </RelativeLayout>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/messageText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="32dp"
                android:background="@drawable/ic_calender"
                android:scrollbars="vertical" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@android:drawable/ic_menu_send" />

        </RelativeLayout>
    </LinearLayout>
</LinearLayout>
</ScrollView>

并在您的清单文件中

        <activity
        android:name=".Activity"
        android:theme="@style/SeachTheme"
        android:windowSoftInputMode="adjustResize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>