即使Android工作室预览很好,Android studio scrollview也没有在模拟器或手机中显示任何内容。甚至缺少背景颜色。虽然,我是Android工作室的菜鸟,但我认为一切都应该在这个代码中工作,因此我很困惑。
我已经阅读了关于这个问题的多个主题,但是他们没有充分回答这个问题。按照这些答案的指示,我试图调整宽度和高度设置,并使用线性布局作为根,以及其他技巧,但收效甚微。但是,我认为线性布局没有阻止scrollview,问题出在这个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 {
}
并且还有Manifest代码
<?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>
我希望scrollview有一个背景颜色和我定义的其他内容。
这里
根据您的问题,您没有onCreate()方法。您的Java类和XML文件之间没有链接。这就是您什么也看不见的原因。
像这样将onCreate添加到您的StartGame类。
public class StartGame extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml);
}
}
像这样更新你的滚动视图,使高度和宽度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>