Android 在自定义view中动态设置布局规则

private RelativeLayout.LayoutParams layoutParams;
 
  
 
  
 
  
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.bottomMargin = -dip2px(context,200);
setLayoutParams(layoutParams);

 
  
 
  
 
  
public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

你可能感兴趣的:(Android 在自定义view中动态设置布局规则)