获取/设置android小部件图像以编程方式查看大小
问题描述:
我有一个小部件。并在其中的图像视图;我想以编程方式获取/设置ImageView的大小;对于像relativeLayout这样的其他视图以及.....如何做到这一点? 谢谢获取/设置android小部件图像以编程方式查看大小
答
**get the width and height of an android.widget.ImageView**
int finalHeight, finalWidth;
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
final TextView tv = (TextView)findViewById(R.id.size_label);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = iv.getMeasuredHeight();
finalWidth = iv.getMeasuredWidth();
tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
return true;
}
});
// if not declare in xml layout
myImageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
**Set ImageView width and height programmatically**
image_view.getLayoutParams().height = 20;
谢谢。但它没有findViewById :( – 2014-09-06 12:57:11
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30,30); myImageView.setLayoutParams(layoutParams); – 2014-09-06 13:02:46
如何在widget上找到imageview? – 2014-09-06 13:20:32