我们如何在android中为imageview设置高度和宽度dp?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20948793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we set height and width dp for imageview in android?
提问by Karthik
I would like to set height and width in dp
for ImageView
in android pragmatically.
我想设置高度和宽度dp
的ImageView
android系统中务实。
How can I achieve this?
我怎样才能做到这一点?
回答by Andros
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>
回答by sham
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);
回答by velval
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>
回答by Andreas Constantinou
- at first choose a desirable dip and assign it to a var.
- 首先选择一个理想的倾角并将其分配给一个变量。
int dipAmount=350;
int dipAmount=350;
- Then read the height of your ImageView.
- 然后读取 ImageView 的高度。
float scale = imageview.Resource.DisplayMetrics.Density;
float scale = imageview.Resource.DisplayMetrics.Density;
- Convert from px to dip.
- 从px转换为dip。
int converter =(int) (350 * scale + 0.5f);
int converter =(int) (350 * scale + 0.5f);
- Set the height to imageview.
- 将高度设置为 imageview。
imageView.LayoutParameters.Height=converter;
imageView.LayoutParameters.Height=converter;
回答by lalith
This may help you...
这可能会帮助你...
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = 100;
params.width = 100;
回答by Tejas Gawali
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);
回答by Satyaki Mukherjee
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.
如果可以接受,请给我您的反馈。这对我有用。