如何避免元素重叠显示RelativeLayout上的软键盘

问题描述 投票:3回答:3

我在这里看过很多帖子,似乎无法找到如何做到这一点。

我正在尝试创建以下布局xml。

在中间我想要一个LinearLayout内部和在LinearLayout的顶部和开头之间的一些按钮我想要一个textView

LinearLayout位于RelativeLayout内的ScrollView内

我通过将LinearLayoutTextView放入RelativeLayout来实现这一目标。 LinearLayout位于父母的中心,textView位于linearLayout之上并且位于父级的顶部。文本垂直居中。

问题是当keyboard开启时。我只把LinearLayout放在ScrollView里面。如果我在adjustPan使用manifest然后没有任何反应,我不能scroll。如果我使用adjustResize然后Textview出现在LinearLayout顶部而不是在上面。那怎么办呢?还是不可能?如果需要,我只想要linearLayout成为scrollable。这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/black_overlay"
    android:animateLayoutChanges="true"
    android:hapticFeedbackEnabled="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_centerInParent="true"
        android:id="@+id/scrollView">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_horizontal"
            android:id="@+id/mLayout">

            <Button
                android:layout_marginBottom="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Match"
                android:id="@+id/btn_newMatch"
                style="@style/ButtonBar"
                android:typeface="monospace"/>

            <Button
                android:layout_marginBottom="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Manage Matches"
                android:id="@+id/btn_manageMatches"
                style="@style/ButtonBar"
                android:typeface="monospace"/>

            <Button
                android:layout_marginBottom="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Manage Teams"
                android:id="@+id/btn_manageTeams"
                style="@style/ButtonBar"
                android:typeface="monospace"/>

            <Button
                android:layout_marginBottom="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Settings"
                android:id="@+id/btn_settings"
                style="@style/ButtonBar"
                android:typeface="monospace"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Logout"
                android:id="@+id/btn_logout"
                style="@style/ButtonBar"
                android:typeface="monospace"/>
        </LinearLayout>
    </ScrollView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Name"
        android:typeface="monospace"
        android:layout_above="@id/scrollView"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:textSize="50dp"
        android:textAppearance="?android:textAppearanceMediumInverse"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"/>
</RelativeLayout>

这是adjustResize目前的样子:

没有键盘:

enter image description here

使用键盘:

enter image description here

任何人都能指出我正确的方向吗?

谢谢!!! :)

android android-layout android-softkeyboard android-xml android-scrollview
3个回答
0
投票

加上这个

android:windowSoftInputMode="adjustPan|stateHidden"

在您的清单中的活动声明中。


0
投票
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:background="@android:color/darker_gray"
    android:hapticFeedbackEnabled="true" >

    <EditText
        android:id="@+id/etName"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etName"
        android:layout_centerInParent="true"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/mLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btn_newMatch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:text="New Match"
                android:typeface="monospace" />

            <Button
                android:id="@+id/btn_manageMatches"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:text="Manage Matches"
                android:typeface="monospace" />

            <Button
                android:id="@+id/btn_manageTeams"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:text="Manage Teams"
                android:typeface="monospace" />

            <Button
                android:id="@+id/btn_settings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:text="Settings"
                android:typeface="monospace" />

            <Button
                android:id="@+id/btn_logout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Logout"
                android:typeface="monospace" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

在你的Manifest活动标签

 android:windowSoftInputMode="adjustResize|stateHidden"

0
投票

当键盘显示时,为了防止我的RelativeLayout中视图的垂直重叠 - 只需添加一个边距即可解决问题

android:layout_marginTop="25dp"

© www.soinside.com 2019 - 2024. All rights reserved.