Android: Align Parent Bottom + Bottom margin programmatically

0

Does anyone know how I can programmatically add a RelativeLayout lined up at the bottom of the parent and include a border or padding at the bottom of that RelativeLayout itself?

Eg:

    
asked by anonymous 25.08.2014 / 16:06

1 answer

1

To add programmatically I used the following code:

RelativeLayout r = new RelativeLayout(this);

// O padding eh setado direto na View
r.setPadding(100, 100, 100, 100);

// Recupero o pai
RelativeLayout rl = ((RelativeLayout) findViewById(R.id.parent));

// Soh para dar contraste com o background
rl.setBackgroundColor(getResources().getColor(R.color.red));

// Crio um LayoutParams para posicionar e dar tamanho para a View
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(400, 400);
// ou
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT
);

// Setando as margens
lp.setMargins(0, 0, 32, 32);

// Adiciono uma regra para alinhar o filho ao fundo do pai
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// Adiciono as regras para alinha o filho no final do pai
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// Suporte para layout RTL
lp.addRule(RelativeLayout.ALIGN_PARENT_END);

// Adiciono a View no pai, especificando o LayoutParams
rl.addView(r, lp);

Of course, to give the expected effect, the parent needs to occupy all space, using MATCH_PARENT relative to Window .

An example like it was on my device:

    
25.08.2014 / 17:28