如何以编程方式获取和设置 ImageButton 的大小?Android似乎使按钮尺寸大于图像尺寸。也许我错了。我想让它的大小与图像大小相同。
问问题
1777 次
3 回答
3
try to set padding to 0dp in xml layout:
<ImageButton ...
android:padding="0dp" />
The image should be as big as button. If you want to make button (but not an image) transparent set button background color to @null:
<ImageButton ...
android:background="@null" />
于 2012-04-23T07:23:58.630 回答
0
int height = button.getLayoutParams().height;
int width = button.getLayoutParams().width;
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
于 2012-04-23T07:27:44.640 回答
0
以编程方式获取高度和宽度:
ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams();
int height = layoutParams.height;
int width = layoutParams.width;
以编程方式设置高度和宽度:
int myHeight = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicHeight();
/*any desired height, but for OP's sake ^^*/
int myWidth = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicWidth();
/*any desired width, but for OP's sake ^^*/
ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams();
layoutParams.height = myHeight;
layoutParams.width = myWidth;
mImageButton.setLayoutParams(layoutParams);
于 2017-08-17T13:43:19.150 回答