主要是 一些属性设置不同
LinearLayout
LinearLayout linearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.
LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
param.setMargins(0, 0, 0, 0);
RelativeLayout
RelativeLayout rl = new RelativeLayout(context);
//定义一个RelativeLayout组件
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//与父组件顶部对齐
lp1.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP);
//横向居中,是
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, true);
// btn1 位于父 View 的顶部,在父 View 中水平居中