我有一个RelativeLayout,我试图添加向下滚动的功能 - 当设备处于水平位置时。 (目前 - 水平翻转时按钮会消失 - 这是不受欢迎的。)
<?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 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
答案 0 :(得分:4)
ScrollView
只能有一个子元素。你有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>
答案 1 :(得分:1)
ScrollView不能在其中添加两个以上的视图....所以最好在ScrollView中使用它,你可以使用一个LinearLayout或RelativeLayout并在其中添加其他布局。最后在其中添加子视图。 ..
答案 2 :(得分: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>