如何在Android中以编程方式调整ImageView的大小?

时间:2012-01-05 14:47:10

标签: android android-layout imageview

请使用以下代码:

ImageView imageView = new ImageView(activity);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

运行此操作会导致我的应用崩溃。我不能为我的生活弄清楚如何设置在代码中创建的ImageView的高度/重量。

01-05 14:46:34.593: E/AndroidRuntime(12038): FATAL EXCEPTION: main
01-05 14:46:34.593: E/AndroidRuntime(12038): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.Gallery.setUpChild(Gallery.java:772)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.Gallery.makeAndAddView(Gallery.java:741)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.Gallery.layout(Gallery.java:625)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.Gallery.onLayout(Gallery.java:339)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.View.layout(View.java:7175)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.View.layout(View.java:7175)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.ScrollView.onLayout(ScrollView.java:1296)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.View.layout(View.java:7175)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.View.layout(View.java:7175)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.View.layout(View.java:7175)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.View.layout(View.java:7175)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.os.Looper.loop(Looper.java:130)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at android.app.ActivityThread.main(ActivityThread.java:3683)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at java.lang.reflect.Method.invoke(Method.java:507)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-05 14:46:34.593: E/AndroidRuntime(12038):    at dalvik.system.NativeStart.main(Native Method)

对于记录,我正在创建的图像视图是在BaseAdapter类的getView方法中创建的,然后由Gallery使用。

1 个答案:

答案 0 :(得分:2)

我认为你在LinearLayout中有ImageView,但是你正在为RelativeLayout传递LayoutParams。请试试这个。

ImageView imageView = new ImageView(activity);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));