0

我有一个 RelativeLayout,我正在尝试添加向下滚动的功能 - 当设备处于水平位置时。(目前 - 当您水平翻转按钮时它会消失 - 这是不希望的。)

当前 XML 状态:

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



    <Button
        android:id="@+id/go_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:background="@drawable/apn_app_go_button" />

    <ImageView
        android:id="@+id/emblem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:scaleType="fitStart"
        android:src="@drawable/apn_app_logo" />

     <TextView
         android:id="@+id/text"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/emblem"
         android:layout_centerHorizontal="true"
         android:gravity="center"
         android:text="@string/start_text"
         android:textColor="#000000"
         android:textSize="14sp" />



</RelativeLayout>

问题是当我尝试将上面的 XML 更改为 ScrollView 时:

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



    <Button
        android:id="@+id/go_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:background="@drawable/apn_app_go_button" />

    <ImageView
        android:id="@+id/emblem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:scaleType="fitStart"
        android:src="@drawable/apn_app_logo" />

     <TextView
         android:id="@+id/text"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/emblem"
         android:layout_centerHorizontal="true"
         android:gravity="center"
         android:text="@string/start_text"
         android:textColor="#000000"
         android:textSize="14sp" />



</ScrollView>

我得到一个致命的例外:

08-23 11:06:52.419: E/AndroidRuntime(3919): FATAL EXCEPTION: main
08-23 11:06:52.419: E/AndroidRuntime(3919): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.new.datasettings/com.project.new.datasettings.StartActivity}: java.lang.IllegalStateException: ScrollView can host only one direct child
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.os.Looper.loop(Looper.java:137)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.ActivityThread.main(ActivityThread.java:4429)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at java.lang.reflect.Method.invoke(Method.java:511)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at dalvik.system.NativeStart.main(Native Method)
08-23 11:06:52.419: E/AndroidRuntime(3919): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.widget.ScrollView.addView(ScrollView.java:241)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:252)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.Activity.setContentView(Activity.java:1841)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at com.project.new.datasettings.StartActivity.onCreate(StartActivity.java:44)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.Activity.performCreate(Activity.java:4471)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-23 11:06:52.419: E/AndroidRuntime(3919):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
08-23 11:06:52.419: E/AndroidRuntime(3919):     ... 11 more
4

3 回答 3

4

AScrollView只能有一个子元素。您有 3 个,即 Button、ImageView 和 TextView。尝试RelativeLayout用一个包装你ScrollView

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

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >



<Button
    android:id="@+id/go_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="85dp"
    android:background="@drawable/apn_app_go_button" />

<ImageView
    android:id="@+id/emblem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:scaleType="fitStart"
    android:src="@drawable/apn_app_logo" />

 <TextView
     android:id="@+id/text"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/emblem"
     android:layout_centerHorizontal="true"
     android:gravity="center"
     android:text="@string/start_text"
     android:textColor="#000000"
     android:textSize="14sp" />



 </RelativeLayout>
 </ScrollView>
于 2013-08-23T15:18:11.950 回答
1

ScrollView 不能在其中添加两个以上的视图....所以最好在 ScrollView 中使用它,您可以取一个 LinearLayout 或 RelativeLayout 并在其中添加其他布局..最后在其中添加子视图...

于 2013-08-23T15:20:20.180 回答
0

将代码替换为:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >


    <Button
        android:id="@+id/go_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:background="@drawable/apn_app_go_button" />

    <ImageView
        android:id="@+id/emblem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:scaleType="fitStart"
        android:src="@drawable/apn_app_logo" />

     <TextView
         android:id="@+id/text"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/emblem"
         android:layout_centerHorizontal="true"
         android:gravity="center"
         android:text="@string/start_text"
         android:textColor="#000000"
         android:textSize="14sp" />


</RelativeLayout>
</ScrollView>
于 2013-08-23T15:20:19.257 回答