我一直在尝试调整scrollview的大小,但没有成功。我尝试设置android:fillViewport="true"
它没有成功。我再次尝试使用java代码设置它:
scroller = (ScrollView) this.findViewById(R.id.scrollView1);
scroller.setLayoutParams(new LinearLayout.LayoutParams(200, 300));
应用程序崩溃了。我想保持scrollview的大小不变。更确切地说,当文本太大时,scrollview不应该占用所有屏幕,而只占用它的一部分。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mytext"
android:textSize="20sp" />
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerHorizontal="true"
android:text="Ok" />
</RelativeLayout>
</ScrollView>
修改
07-19 15:36:14.570: E/AndroidRuntime(11360): FATAL EXCEPTION: main
07-19 15:36:14.570: E/AndroidRuntime(11360): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bledi.ui/com.bledi.ui.MainActivity}: java.lang.NullPointerException
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.os.Looper.loop(Looper.java:137)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-19 15:36:14.570: E/AndroidRuntime(11360): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 15:36:14.570: E/AndroidRuntime(11360): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 15:36:14.570: E/AndroidRuntime(11360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-19 15:36:14.570: E/AndroidRuntime(11360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-19 15:36:14.570: E/AndroidRuntime(11360): at dalvik.system.NativeStart.main(Native Method)
07-19 15:36:14.570: E/AndroidRuntime(11360): Caused by: java.lang.NullPointerException
07-19 15:36:14.570: E/AndroidRuntime(11360): at com.bledi.ui.MainActivity.onCreate(MainActivity.java:32)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.Activity.performCreate(Activity.java:5104)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-19 15:36:14.570: E/AndroidRuntime(11360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
答案 0 :(得分:2)
崩溃日志中显示的第一点是指在某个时刻未实例化的对象。
我认为如果你想要一个文本滚动,但不要留在预定义的空间内,你可以这样做:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="200dip"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your big text here!"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Ok" />
</LinearLayout>
</LinearLayout>
答案 1 :(得分:1)
如果你想保持scrollview
的大小不变,你应该在布局中定义它的大小:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="200dip"
android:layout_height="300dip" >
........
...........
</ScrollView>
正如您所定义:
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
宽度取屏幕宽度,高度调整为内容高度
答案 2 :(得分:0)
看起来findViewById找不到你的ScrollView。
您是否将setContentView放入onCreate?