以编程方式调整图像按钮的边距,高度和宽度

azh*_*har 6 android android-layout

我有一个图像按钮,其宽度和高度在xml文件中指定.但我想用屏幕尺寸的设备更改它.
如何以编程方式设置ImageButton的宽度,高度和边距?

Leo*_*Dev 5

您好朋友,您可以通过以下代码设置

btnPlayVideo.getLayoutParams().height = XX;
btnPlayVideo.getLayoutParams().width = XX;
Run Code Online (Sandbox Code Playgroud)

或者看到这个图片按钮调整大小


Ham*_*atu 4

//ImageView Setup
ImageView imageView = new ImageView(this);

//setting height and width of imageview
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

//setting margins around imageimageview
params.setMargins(10, 10, 10, 10); //left, top, right, bottom

//adding attributes to the imageview
imageView.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)