以编程方式获取/设置android小部件图像视图大小

时间:2014-09-06 11:39:04

标签: android android-layout android-widget

我有一个小部件。并在其中的图像视图;我想以编程方式获取/设置ImageView的大小;并为其他视图执行此操作,例如relativeLayout和.....我该怎么做? 感谢

1 个答案:

答案 0 :(得分:-1)

**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;