动态改变控件大小

设置比率

float BASE_WIDTH = 320.0f;
 float BASE_SCALE = 0;

Display display = getWindowManager().getDefaultDisplay();
mMetrics = new DisplayMetrics();
display.getMetrics(mMetrics);

BASE_SCALE = mMetrics.widthPixels / BASE_WIDTH;

代码中动态设置TextView

TextView txtLength= (TextView)findViewById(R.id.Length);

LayoutParams laParams=(LayoutParams)txtLength.getLayoutParams();
laParams.width = (int)(110 * BASE_SCALE);
laParams.height = LinearLayout.LayoutParams.FILL_PARENT;
txtLength.setLayoutParams(laParams);
注意该控件的父控件类型。该TextView  被包含于LinearLayout中。


设置Margin边距

MarginLayoutParams margin_layout = new MarginLayoutParams(txtLength.getLayoutParams());
margin_layout.setMargins(0, 0, (int)(9 * BASE_SCALE),(int)(5 * BASE_SCALE));
txtLength.setLayoutParams(new LinearLayout.LayoutParams(margin_layout));
 
 

 
 





你可能感兴趣的:(layout,float)