赞
踩
I would like to set height and width in dp for ImageView in android pragmatically.
我想在android中實際設置ImageView中的高度和寬度。
How can I achieve this?
我怎樣才能做到這一點?
7 个解决方案
#1
47
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中提供鍵的值:
50dp
50dp
#2
23
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);
#3
4
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中
80dp
80dp
#4
1
at first choose a desirable dip and assign it to a var.
首先選擇理想的傾角並將其分配給var。
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;
#5
0
This may help you...
這可能對你有所幫助......
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = 100;
params.width = 100;
#6
0
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);
#7
-2
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.
如果可以接受,請給我你的反饋。這對我有用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。