我正在尝试以编程方式设置布局宽度,这是我的 XML 布局文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/startTime_EndtimeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/HeadingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/HeadingTextView"
android:layout_above="@+id/dialogButtonOK"
android:scrollbars="vertical" >
<RelativeLayout
android:id="@+id/startTime_EndtimeLayoutWithScroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="@+id/startTime_EndtimeTableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableLayout>
</RelativeLayout>
</ScrollView>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text=" close " />
</RelativeLayout>
我想设置相对布局宽度,Scroll View的子视图。我尝试过的方法,
1.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.startTime_EndtimeLayoutWithScroll);
rl.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
2.
RelativeLayout rl = (RelativeLayout)scrollView.findViewById(R.id.startTime_EndtimeLayoutWithScroll);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
在这两种情况下,我都会收到此错误,
10-11 17:36:31.526: E/dalvikvm(1555): [DVM] mmap return base = 4586a000
10-11 17:36:32.824: E/dalvikvm(1555): [DVM] mmap return base = 459e1000
10-11 17:36:32.966: E/AndroidRuntime(1555): FATAL EXCEPTION: main
10-11 17:36:32.966: E/AndroidRuntime(1555): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.widget.FrameLayout.onLayout(FrameLayout.java:324)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.widget.ScrollView.onLayout(ScrollView.java:1331)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:943)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.view.View.layout(View.java:7225)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.view.ViewRoot.performTraversals(ViewRoot.java:1181)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.view.ViewRoot.handleMessage(ViewRoot.java:1913)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.os.Handler.dispatchMessage(Handler.java:130)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.os.Looper.loop(SourceFile:351)
10-11 17:36:32.966: E/AndroidRuntime(1555): at android.app.ActivityThread.main(ActivityThread.java:3820)
10-11 17:36:32.966: E/AndroidRuntime(1555): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 17:36:32.966: E/AndroidRuntime(1555): at java.lang.reflect.Method.invoke(Method.java:538)
10-11 17:36:32.966: E/AndroidRuntime(1555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
10-11 17:36:32.966: E/AndroidRuntime(1555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
10-11 17:36:32.966: E/AndroidRuntime(1555): at dalvik.system.NativeStart.main(Native Method)
解决问题的任何想法。谢谢