【问题标题】:how to make footer rise with keyboard如何用键盘使页脚上升
【发布时间】:2019-10-16 23:16:34
【问题描述】:

当键盘出现时,我希望页脚随着键盘上升。

我尝试添加代码 android:windowSoftInputMode="adjustPan" ,android:windowSoftInputMode="adjustResize" 和 android:windowSoftInputMode="adjustResize|stateHidden" 到 manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:isScrollContainer="true"
    >
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btCancel"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left"
                android:background="#FFFFFF"
                android:text="@string/bt_cancel" />

        </LinearLayout>


        <EditText
            android:layout_width="340dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:background="#00000000"
            android:gravity="top|left"
            android:hint="@string/tv_comment"
            android:inputType="textMultiLine"
            android:maxLength="255"
            android:maxLines="6"
            android:textSize="20sp"
            >
        </EditText>

    </LinearLayout>


</ScrollView>

<Button
    style="?android:textAppearanceSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/action_sign_in_short"
    android:layout_alignParentBottom="true"
    android:textStyle="bold" />

我使代码更简单,但我认为它并不好。所以添加原始代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="true">

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

        <Button
            android:id="@+id/btCancel"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#FFFFFF"
            android:text="@string/bt_cancel" />

        <Button
            android:id="@+id/btContribution"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#FFFFFF"
            android:text="@string/bt_contribution" />

        <EditText
            android:id="@+id/etComment"
            android:layout_width="340dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:background="#00000000"
            android:gravity="top|left"
            android:hint="@string/tv_comment"
            android:inputType="textMultiLine"
            android:maxLength="255"
            android:maxLines="6"
            android:textSize="20sp" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

<Button
    style="?android:textAppearanceSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/action_sign_in_short"
    android:textStyle="bold" />
</RelativeLayout>

【问题讨论】:

    标签: android xml android-studio android-layout


    【解决方案1】:

    我认为您可能走在正确的轨道上,但我怀疑问题在于作为您的 ScrollView 的直系子代存在的 LinearLayout。我相信android:layout_height="wrap_content" 属性可能会导致它不起作用(我可能错了!)。也就是说,我稍微调整了你的 XML 布局(我还没有机会测试它)——你能看看下面的代码是否适合你吗?

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:isScrollContainer="true">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <Button
                    android:id="@+id/btCancel"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:background="#FFFFFF"
                    android:text="@string/bt_cancel" />
    
                <EditText
                    android:layout_width="340dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="70dp"
                    android:background="#00000000"
                    android:gravity="top|left"
                    android:hint="@string/tv_comment"
                    android:inputType="textMultiLine"
                    android:maxLength="255"
                    android:maxLines="6"
                    android:textSize="20sp" />
    
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>
    
        <Button
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="@string/action_sign_in_short"
            android:textStyle="bold" />
    </RelativeLayout>
    

    过去我使用NestedScrollView 取得了更大的成功。请注意,如果您不使用AndroidX,则需要将androidx.core.widget.NestedScrollView 替换为android.support.v4.widget.NestedScrollView

    希望对您有所帮助 - 请告诉我您的进展情况!

    【讨论】:

    • 不是问题@fromjapan!如果我提供的解决方案对您有所帮助,您介意接受我的回答吗?
    猜你喜欢
    • 2014-05-02
    • 2013-07-24
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多