2014-09-06 107 views

回答

-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; 
+0

謝謝。但它沒有findViewById :( – 2014-09-06 12:57:11

+0

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30,30); myImageView.setLayoutParams(layoutParams); – 2014-09-06 13:02:46

+0

如何在widget上找到imageview? – 2014-09-06 13:20:32

相關問題