使用按钮在运行时调整 ImageView 的大小 - Android
我正在使用 ImageView 容器显示图像。当用户输入特定值并单击“更新调整大小”时,需要调整图像大小
,仅用于显示,原始资源保持原样。无需编辑、保存等
xml 布局
<ImageView
android:background="@drawable/picture01"
android:id="@+id/picture01holder"
android:layout_below="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
现在我该怎么做才能动态分配此 pic01 我选择的大小。只是功能,我可以自己在按钮内实现它。我相信我必须使用像 pic01.setLayoutParams 这样的东西,但我不知道如何使用它。
基本上我想覆盖.java中的layout_height =“wrap_content”和layout_width =“wrap_content”
I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update
resize is just for display, the original resource remains as it is. NO editing, save, etc
xml layout
<ImageView
android:background="@drawable/picture01"
android:id="@+id/picture01holder"
android:layout_below="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.
basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过获取
LayoutParams
更改Imageview
大小change the
Imageview
size by takingLayoutParams
首先,您必须设置 图像的缩放类型为 CENTER_INSIDE 或CENTER_CROP取决于您的喜好。您应该在 onCreate() 方法中执行此操作。
之后,您只需调整布局中图像视图的大小即可。这将取决于您使用的布局类型。 LinearLayout 的一个示例是:
或者您可以设置组件最小尺寸:
在这两种情况下,请确保可以在布局内实现该尺寸。如果您有多个重量较小的组件,则图像视图可能无法占用您所需的空间。
First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.
Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:
Or you can set the components minimal size:
In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.