1
我有一個小部件。並在其中的圖像視圖;我想以編程方式獲取/設置ImageView的大小;對於像relativeLayout這樣的其他視圖以及.....如何做到這一點? 謝謝獲取/設置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