简体   繁体   English

我们如何在android中为imageview设置高度和宽度dp?

[英]How can we set height and width dp for imageview in android?

I would like to set height and width in dp for ImageView in android pragmatically. 我想设置的高度和宽度在dpImageView android系统中务实。

How can I achieve this? 我怎样才能做到这一点?

Set width & height with dp : 用dp设置宽度和高度:

imageview.getLayoutParams().height = (int) getResources().getDimension(R.dimen.imageview_height);
imageview.getLayoutParams().width = (int) getResources().getDimension(R.dimen.imageview_width);

In your dimens.xml provide values for the keys : 在dimens.xml中提供键的值:

<dimen name="imageview_width">50dp</dimen> 
<dimen name="imageview_height">50dp</dimen> 

Use display metrics to get the sale factor and then just some simple maths - example, if I want 200x150dp: 使用显示指标来获得销售因子,然后使用一些简单的数学 - 例如,如果我想要200x150dp:

final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx  = (int) (200 * scale);
int dpHeightInPx = (int) (150 * scale);

Then set the image view size: 然后设置图像视图大小:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpWidthInPx, dpHeightInPx);
imageView.setLayoutParams(layoutParams);

This may be simpler and should do the trick: 这可能更简单,应该做的伎俩:

ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = getActivity().getResources().getDimensionPixelSize(R.dimen.item_height);
params.width = getActivity().getResources().getDimensionPixelSize(R.dimen.item_width);

And in your dimens.xml 在您的dimens.xml中

<dimen name="item_height">80dp</dimen> 
<dimen name="item_width">80dp</dimen> 

This may help you... 这可能对你有所帮助......

ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = 100;
params.width = 100;
  1. at first choose a desirable dip and assign it to a var. 首先选择理想的倾角并将其分配给var。

int dipAmount=350; int dipAmount = 350;

  1. Then read the height of your ImageView. 然后读取ImageView的高度。

float scale = imageview.Resource.DisplayMetrics.Density;

  1. Convert from px to dip. 从px转换为dip。

int converter =(int) (350 * scale + 0.5f);

  1. Set the height to imageview. 将高度设置为imageview。

imageView.LayoutParameters.Height=converter;

 final float scale = getContext().getResources().getDisplayMetrics().density;
int height_ = (int) (250 * scale + 0.5f);
int width_ = (int) (250 * scale + 0.5f);

250 is in dp 250是dp

ViewGroup.LayoutParams params = ImageView.getLayoutParams();
params.height = height_;
params.width = width_;

ImageView.setLayoutParams(params);

Try this: 尝试这个:

image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width= 20;

Give me your feedback if it's acceptable. 如果可以接受,请给我你的反馈。 It's work for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android中以编程方式设置ImageView的高度宽度? - How to set Height Width of ImageView programmatically in android? 如何在活动Android中设置ImageView的高度和宽度 - How to set Imageview height and width in activity android Android-ImageView-多屏支持-将高度/宽度设置为dp还是match_parent / wrap_content? - Android - ImageView - Multiple Screen Support - Set height/width as dp or match_parent/wrap_content? 如何通过PX在DP中设置高度和宽度 - How to set height and width in DP from PX 如何在dp值中设置Layoutparams的高度/宽度? - How to set Layoutparams height/width in dp value? 如何在XML上将ImageView的宽度和高度设置为另一个ImageView的宽度和高度? - How can I set the width and height of an ImageView to another ImageView's width and height on the XML? 如何在Android中使用onTouch设置imageview的高度和宽度 - how to set imageview's height and width using onTouch in android 如何在代码中将android:width设置为0dp? - How to set android:width to 0dp in code? 以编程方式设置扩展ImageView的宽度和高度-android - set extended ImageView width and height programatically - android 如何以编程方式获取一个ImageView的高度和宽度,并将其设置为android中的另一个ImageView - How to programmatically get the height & width of one ImageView and set it to another ImageView in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM