Android:以编程方式alignment父级底部和底部边距

我怎样才能编程方式添加一个RelativeLayoutalignment到父母的底部,并添加一个边缘或填充在相同的RelativeLayout?

例:

在这里输入图像说明

Solutions Collecting From Web of "Android:以编程方式alignment父级底部和底部边距"

你可以这样做:

// Get the parent layout RelativeLayout parent = (RelativeLayout) findViewById(R.id.parent); // Create your custom layout RelativeLayout relativeLayout = new RelativeLayout(this); // Create LayoutParams for it // Note 200 200 is width, height in pixels RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200, 200); // Align bottom-right, and add bottom-margin params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.bottomMargin = 100; relativeLayout.setLayoutParams(params); relativeLayout.setBackgroundColor(Color.BLUE); // Add the custom layout to your parent layout parent.addView(relativeLayout); 

你可以试试这个在我的情况下工作:

 RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams(); // position on right bottom rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0); rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);