我是android开发的初学者,我想在我的应用程序中进行滚动视图,因为高度不够,但当我添加消息“应用程序停止工作”时出现
如果没有它,布局就不会完全显现
Xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/imageView1" >
<Space
android:layout_width="1dp"
android:layout_height="3dp" />
<Button
android:id="@+id/button1"
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="@string/b1" />
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:paddingTop="20px">
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20px">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b2" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b3" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b4" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b5" />
<Space
android:layout_width="47dp"
android:layout_height="15dp" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b7" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b8" />
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b9" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20px">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<Space
android:layout_width="47dp"
android:layout_height="15dp" />
<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b10" />
<Space
android:layout_width="47dp"
android:layout_height="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<Button
android:id="@+id/button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b11" />
<Space
android:layout_width="47dp"
android:layout_height="15dp" />
<Button
android:id="@+id/button12"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b12" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<Space
android:layout_width="47dp"
android:layout_height="15dp" />
<Button
android:id="@+id/button13"
style="?android:attr/buttonStyleSmall"
android:layout_width="47dp"
android:layout_height="40dp"
android:text="@string/b13" />
<Space
android:layout_width="47dp"
android:layout_height="15dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:paddingTop="20px">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<TextView
android:id="@+id/textView1"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="@string/s1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="95dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button14"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b14" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<TextView
android:id="@+id/textView2"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="@string/s2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText2"
android:layout_width="95dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button15"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b15" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<TextView
android:id="@+id/textView3"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="@string/s3"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText3"
android:layout_width="95dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button16"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b16" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<TextView
android:id="@+id/textView4"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="@string/s4"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText4"
android:layout_width="95dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button17"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b17" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<TextView
android:id="@+id/textView5"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="@string/s5"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText5"
android:layout_width="95dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button18"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b18" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<TextView
android:id="@+id/textView6"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="@string/s6"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText6"
android:layout_width="95dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button19"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b19" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<TextView
android:id="@+id/textView7"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:text="@string/s7"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText7"
android:layout_width="95dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button20"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b20" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="20px">
<Space
android:layout_width="95dp"
android:layout_height="15dp" />
<Button
android:id="@+id/button21"
style="?android:attr/buttonStyleSmall"
android:layout_width="95dp"
android:layout_height="40dp"
android:text="@string/b21" />
<Space
android:layout_width="95dp"
android:layout_height="15dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
log cat:
05-18 13:12:59.009: E/AndroidRuntime(1492): FATAL EXCEPTION: main
05-18 13:12:59.009: E/AndroidRuntime(1492): java.lang.RuntimeException: Unable tostart activityComponentInfo{com.example.aurduino.quad/com.example.aurduino.quad.MainActivity}: java.lang.IllegalStateException: ScrollView can host only one direct child
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.os.Looper.loop(Looper.java:137)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-18 13:12:59.009: E/AndroidRuntime(1492): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 13:12:59.009: E/AndroidRuntime(1492): at java.lang.reflect.Method.invoke(Method.java:525)
05-18 13:12:59.009: E/AndroidRuntime(1492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-18 13:12:59.009: E/AndroidRuntime(1492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-18 13:12:59.009: E/AndroidRuntime(1492): at dalvik.system.NativeStart.main(Native Method)
05-18 13:12:59.009: E/AndroidRuntime(1492): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.widget.ScrollView.addView(ScrollView.java:250)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-18 13:12:59.009: E/AndroidRuntime(1492): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.Activity.setContentView(Activity.java:1895)
05-18 13:12:59.009: E/AndroidRuntime(1492): at com.example.aurduino.quad.MainActivity.onCreate(MainActivity.java:29)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.Activity.performCreate(Activity.java:5133)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-18 13:12:59.009: E/AndroidRuntime(1492): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-18 13:12:59.009: E/AndroidRuntime(1492): ... 11 more
答案 0 :(得分:0)
尝试删除
android:layout_below="@+id/imageView1"
ScrollView
的,因为您没有ImageView
此外,将整个内容包裹在ScrollView
内LinearLayout
正如docs所说
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.