Android– 如何以编程方式更改ImageButton的宽度?

Android– 如何以编程方式更改ImageButton的宽度?,第1张

概述我有一个ImageButton.代码是–ImageButtonshareButton=newImageButton(this);shareButton.setBackgroundResource(android.R.drawable.ic_menu_share);RelativeLayout.LayoutParamsshareParams=newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutPa

我有一个Imagebutton.代码是 –

Imagebutton sharebutton = new Imagebutton(this);sharebutton.setBackgroundResource(androID.R.drawable.ic_menu_share);relativeLayout.LayoutParams shareParams = new relativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);shareParams.addRule(relativeLayout.AliGN_PARENT_RIGHT, navigationlogo.getID());sharebutton.setLayoutParams(shareParams);

我需要改变它的宽度.但是Imagebutton和布局参数都没有setWIDth方法.在没有答案的情况下在网上看了很多.

解决方法:

您可以使用实际数字,而不是使用relativeLayout.WRAP_CONTENT作为宽度,该数字将是按钮的新宽度(以像素为单位).由于您可能希望将dp中的宽度指定为与分辨率无关,因此您可能需要使用如下方法将dp转换为像素:

public static int dptopixels(Context context, float dp) {    final float scale = context.getResources().getdisplayMetrics().density;    return (int) (dp * scale + 0.5f);}
总结

以上是内存溢出为你收集整理的Android – 如何以编程方式更改ImageButton的宽度?全部内容,希望文章能够帮你解决Android – 如何以编程方式更改ImageButton的宽度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114087.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存