我正在尝试在我的活动中实用地为我的相对布局添加一个边距顶部。使用 xml 我可以在这种模式下做到这一点:android:layout_marginTop="10dp"
但是当我试图务实地做到这一点时,没有任何改变......
这是我正在使用的代码:
//SCROLL VIEW
ScrollView scrollView = new ScrollView(this);
scrollView.setBackground(getResources().getDrawable(R.drawable.background));
scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.MATCH_PARENT));
scrollView.setPadding(20, 20, 20, 20);
//LINEAR LAYOUT
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
for (int i=1; i<=3; i++){
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
head_params.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(head_params);
//Need to understand how put a margin top to the relativeLayout
//IMAGE VIEW
ImageView selectedPhoto = new ImageView(this);
selectedPhoto.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
selectedPhoto.setImageResource(R.drawable.ic_launcher);
//TEXT VIEWS
TextView numberCopies = new TextView(this);
numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
numberCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
numberCopies.setText("2 copies ");
RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
numberCopies.setLayoutParams(layoutParamsNumberCopies);
TextView priceCopies = new TextView(this);
priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
priceCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
priceCopies.setTextColor(getResources().getColor(R.color.redColor));
priceCopies.setText("$ 25 ");
RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
priceCopies.setLayoutParams(layoutParamsPriceCopies);
relativeLayout.addView(selectedPhoto);
relativeLayout.addView(numberCopies);
relativeLayout.addView(priceCopies);
linearLayout.addView(relativeLayout);
}
scrollView.addView(linearLayout);
setContentView(scrollView);
}
我也尝试过relativeLayout.setMargins(20, 0, 0, 0);
,但似乎此方法不适用于 relativeLayout。我不确定。
谢谢