Scrollview 未在模拟器或 phone 中显示任何内容,即使预览看起来不错已解决

Scrollview is not showing any content in emulator or phone even though the preview is looking fine SOLVED

Android studio scrollview 未在模拟器或 phone 中显示任何内容,即使 Android studio 预览正常。连背景颜色都不见了。虽然,我是 Android studio 的菜鸟,但我认为一切都应该在这段代码中工作,因此我很困惑。

我已经阅读了多个关于这个问题的讨论帖,但没有一个充分回答了这个问题。按照这些答案的指示,我尝试调整宽度和高度设置,并使用线性布局作为根,以及其他技巧,但收效甚微。但是,我认为线性布局不会阻塞滚动视图,问题出在这个 xml 文件或其他地方。我已经尝试自己至少花两个小时解决这个问题,但我正在向更高级的开发人员寻求一些建议。

问题是我在 Java 文件中没有 onCreate 方法。

<?xml version="1.0" encoding="utf-8"?>
<!--
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
tools:context=".StartGame">
-->



<ScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/background"
tools:context=".StartGame">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/eka_teksti"
            android:textAlignment="center"
            android:textColor="#000"
            android:layout_marginTop="2dp"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/ic_launcher" />


    </LinearLayout>
</ScrollView>

<!--
</LinearLayout>
-->

这是相关的Java代码


    package com.jussitamminen.pplpankki;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.content.Intent;
    import android.view.View;

    public class StartGame extends AppCompatActivity {


    }

还有清单代码


    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.jussitamminen.pplpankki">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".StartGame"
                android:label="Second Activity"
                android:parentActivityName=".MainActivity">

            </activity>
        </application>

    </manifest>

我希望滚动视图具有我定义的背景颜色和其他内容。

此处

像这样更新滚动视图,设置高度和宽度 match_parent 并添加 fillViewport= true

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

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="@string/eka_teksti"
        android:textAlignment="center"
        android:textColor="#000"
        android:layout_marginTop="2dp"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher" />


</LinearLayout>
</ScrollView>

根据您的问题,您没有 onCreate() 方法。您的 Java class 和 XML file.That 之间没有链接,这就是您什么也没看到的原因。

像这样将 onCreate 添加到您的 StartGame class。

public class StartGame extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_xml);
    }
}