【发布时间】:2014-09-06 11:39:03
【问题描述】:
我有一个小部件。以及其中的图像视图;我想以编程方式获取/设置 ImageView 的大小;并为其他视图执行此操作,例如 relativeLayout 和.....我该怎么做? 谢谢
【问题讨论】:
标签: android android-layout android-widget
我有一个小部件。以及其中的图像视图;我想以编程方式获取/设置 ImageView 的大小;并为其他视图执行此操作,例如 relativeLayout 和.....我该怎么做? 谢谢
【问题讨论】:
标签: android android-layout android-widget
**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;
【讨论】: