/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int dip(Context context,int pxValue) {
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,pxValue, context.getResources().getDisplayMetrics());
}
xml布局与动态代码结合,取得需要的layout
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout linearLayout2 = (LinearLayout)inflater.inflate(R.layout.chinesean_detail, null);
1、android:layout_margin 的动态设置:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
lp.setMargins(20,20,20,20);
xxxLayout.addView(xxxView,lp);
2、android:layout_weight:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1.0f);
这里的第三个参数就是设置android:layout_weight的属性
3、android:layout_gravity="center_vertical"设置
LinearLayout.LayoutParams layoutParamsImage = new LinearLayout.LayoutParams(dip(context,50),dip(context,50),2.0f);
layoutParamsImage.gravity = Gravity.CENTER_VERTICAL;
4、设置文字粗体 android:textStyle="bold"
textView1.getPaint().setFakeBoldText(true);