【问题标题】:get/set android widget image view size programmatically以编程方式获取/设置 android 小部件图像视图大小
【发布时间】:2014-09-06 11:39:03
【问题描述】:

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

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案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;
    

    【讨论】:

    • LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30); myImageView.setLayoutParams(layoutParams);
    • 如何在小部件上找到图像视图?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多