Java Android:以编程方式向relativeLayout添加底部边框?

Java Android:以编程方式向relativeLayout添加底部边框?,java,android,android-layout,Java,Android,Android Layout,有人能解释一下如何通过编程将底部边框添加到相对布局中吗? PS:我使用以下代码将边框添加到相对布局中: RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.borderEffect); ShapeDrawable rectShapeDrawable = new ShapeDrawable(); Paint paint = rectShapeDrawable.getPaint(); paint.setColor(Col

有人能解释一下如何通过编程将底部边框添加到相对布局中吗?
PS:我使用以下代码将边框添加到相对布局中:

RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.borderEffect); 
ShapeDrawable rectShapeDrawable = new ShapeDrawable();

Paint paint = rectShapeDrawable.getPaint();
paint.setColor(Color.GRAY);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
layout.setBackgroundDrawable(rectShapeDrawable);
View bottomBorder = new View(CONTEXT);
bottomBorder.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 1);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

relativeLayout.addView(bottomBorder, params);
上面的代码为所有角添加边框,但我只想为底部添加边框。

有什么方法或等效方法可以做到这一点吗?

您可以将视图添加到相对布局中:

RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.borderEffect); 
ShapeDrawable rectShapeDrawable = new ShapeDrawable();

Paint paint = rectShapeDrawable.getPaint();
paint.setColor(Color.GRAY);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
layout.setBackgroundDrawable(rectShapeDrawable);
View bottomBorder = new View(CONTEXT);
bottomBorder.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 1);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

relativeLayout.addView(bottomBorder, params);
选中此项: