Android 如何添加ScrollView? ScrollView 乱七八糟的布局

标签 android layout scrollview shift

我在将 ScrollView 添加到我的布局时遇到问题。因此,当我将 ScrollView (match_parent) 添加到我的布局中时,它会将我的布局移到右侧。因此,我只能在设备屏幕上看到背景图片。所有 Buttons、Textviews 和 Edittexts 都向右移动。顺便说一下,我只为纵向布局编写了代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rauf.myapplication.Main2Activity"
android:background="@drawable/bii3">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp"
    android:paddingLeft="60dp"
    android:paddingRight="58dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingTop="42dp">

        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:src="@drawable/kamera" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_weight="0.5"
        android:paddingTop="35dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/message" />

            <EditText
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:background="#00000000"
                android:hint="email address"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">
            <ImageView
                android:id="@+id/log"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/usrusr"/>
            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:background="#00000000"
                android:hint="username"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">
            <ImageView
                android:id="@+id/red"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/pswrd"/>
            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:background="#00000000"
                android:hint="password"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">
            <ImageView
                android:id="@+id/red2"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/pswrd"/>
            <EditText
                android:id="@+id/passwordconf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:background="#00000000"
                android:hint="password confirm"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_background2"
            android:text="SIGN UP"
            android:onClick="clickButton2"
            android:clickable="true"
            android:layout_marginTop="20dp"
            android:textColor="#ffffff"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="30dp"
            android:onClick="clickFunction2"
            android:text="Already have an Account ?"
            android:textColor="#000000" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

为此我需要 ScrollView:

desired

但我最终得到的是:

enter image description here

最佳答案

这需要是前三个布局的设置 您将最后一个设置为 600dp,这就是为什么它将所有内容进一步向下推,将其更改为 wrap_content:)

  <ScrollView 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_width="match_parent"
                  android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp"
            android:paddingLeft="60dp"
            android:paddingRight="58dp">

            <LinearLayout
                android:id="@+id/CHANGE_THIS_ONE"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" <<<<<<<<<
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingTop="42dp">

关于Android 如何添加ScrollView? ScrollView 乱七八糟的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47289511/

相关文章:

android - 使用 ScrollView 的 Flex 布局

android - 以编程方式从android中的自定义组件创建 ScrollView

xaml - 网格不占用 100% 宽度

android - 如何从线性布局android获取特定位置xy坐标?

java - 如何使用 Thread 像 switch 一样工作

java - 我如何在 libgdx 中将图像旋转到其 x 轴上?

android - 在 Android Studio 中集成 KSOAP 库的正确方法是什么?

java - 如何通过json将包含图像的对象发送到服务器

java - Jframe 中的 JPanel 未显示

c# - 用于 ScrollViewer 中图像动态网格的布局