简体   繁体   中英

How to resize an ImageView programmatically in Android?

Take the following code:

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

Running this crashes my app. I can't for the life of me work out how to set the height/weight of an ImageView created in code.

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)

For the record the image view I'm creating is created within the getView method of the BaseAdapter class which is then used by a Gallery.

I think you have ImageView in LinearLayout but you are passing LayoutParams for RelativeLayout. Please try this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM