在oncreate中,使用getheight等函數不能得到控件大小,獲得的爲0
ide
應使用其餘方法函數
Resources res = getResources();佈局
final ImageView view=(ImageView)findViewById(R.id.IV_about);spa
final Drawable drawable=res.getDrawable(R.drawable.ic_about);server
DisplayMetrics dm = new DisplayMetrics();get
getWindowManager().getDefaultDisplay().getMetrics(dm);it
final int screen_height = dm.heightPixels ;//高度class
ViewTreeObserver vto = view.getViewTreeObserver(); List
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { float
public boolean onPreDraw() {
int height = (int) ((float) view.getMeasuredWidth()/drawable.getMinimumWidth() * drawable.getMinimumHeight());
int width = view.getMeasuredWidth();
LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) view.getLayoutParams(); //取控件textView當前的佈局參數
linearParams.height = height;
linearParams.width = view.getMeasuredWidth();
linearParams.topMargin=screen_height/10;
view.setLayoutParams(linearParams);
view.setImageResource(R.drawable.ic_about);