如何使用ScrollView设置布局?

问题描述 投票:0回答:2

我正在学习Android,现在ScrollView出现问题。

[我将ScrollView用作布局的主要容器,而将LinearLayout用作孩子,因为我希望屏幕支持纵向和横向方向,但是这种方式无法正常工作。

如您在下面的图像中看到的,在图像出现之前没有空格,即使图像被剪切也是如此,这是因为键盘向上推动了布局并且它不适合屏幕,而且在横向方向上也不适合。

有人可以帮助我如何设置布局以支持滚动而不丢失布局的原始设计吗?

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/gradient_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

        <LinearLayout
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            tools:context=".LoginActivity"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="30dp">

            <ImageView
                android:id="@+id/sign_in_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/trivia_wars_logo"
                android:layout_gravity="center"
                android:rotation="-8"
                android:scaleX="0.75"
                android:scaleY="0.75"
                android:padding="5dp"/>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/sign_in_input_layout_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/TextInputLayoutStyle"
                android:layout_marginTop="15dp">

                <EditText
                    android:id="@+id/sign_in_input_username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_email_username"
                    style="@style/EditTextStyle"
                    android:inputType="textPersonName"/>

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/sign_in_input_layout_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/PasswordInputLayoutStyle"
                android:layout_marginTop="15dp"
                app:passwordToggleEnabled="true">

                <EditText
                    android:id="@+id/sign_in_input_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_password"
                    style="@style/EditTextStyle"
                    android:inputType="textPassword"/>

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.button.MaterialButton
                android:id="@+id/sign_in_action_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:paddingTop="12dp"
                android:paddingBottom="12dp"
                style="@style/RoundedButtonStyle"
                android:text="@string/sign_in"/>

            <com.google.android.material.button.MaterialButton
                android:id="@+id/sign_in_sign_up_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="12dp"
                android:paddingBottom="12dp"
                style="@style/OutlineRoundedButtonStyle"
                android:text="@string/sign_up"/>
        </LinearLayout>
</ScrollView>

布局预览:

Preview of the layout

在我的设备中测试过的布局:

![Portrait layout with active keyboard

Landscape layout

感谢。

android android-layout scrollview android-linearlayout android-design-library
2个回答
1
投票

尝试添加

android:windowSoftInputMode="adjustPan" 

在活动内部的清单文件中。

<activity
            android:name=".YourActivity"
            android:windowSoftInputMode="adjustPan">

        </activity>

您将找到有关windowSoftInputMode的信息:here


0
投票

更改您的LinearLayout属性

android:gravity="center"

代替

android:layout_gravity="center"

它将起作用..

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