How to change the width and height of a RelativeLayout by Java code

0

I want to know how I adjust the width and height values of each relative layout independently of my XML through the respective Java class. I already know how to adjust the width, height and other parameters of the buttons present in my layout the way I want, but the buttons are inside relative layouts and these relative layouts need to be tuned and I do not know how to do this. Here is the XML:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="227dp">

    <Button
        android:id="@+id/btnSAE"
        style="@android:style/Widget.Button"
        android:layout_width="220dp"
        android:layout_height="65dp"
        android:background="@drawable/btn_barcode2"
        android:textColorLink="?attr/colorPrimary"
        android:layout_marginBottom="12dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="244dp"
    android:layout_height="75dp">

    <Button
        android:id="@+id/btnBuscarNome"
        style="@android:style/Widget.Button"
        android:layout_width="220dp"
        android:layout_height="65dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/btn_busca" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="244dp"
    android:layout_height="75dp">

    <Button
        android:id="@+id/btnSocorro"
        style="@android:style/Widget.Button"
        android:layout_width="220dp"
        android:layout_height="65dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/btn_socorro" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="383dp"
    android:layout_height="113dp"
    >

    <Button
        android:id="@+id/btnAtualizar"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignTop="@+id/btnSobre"
        android:layout_marginEnd="14dp"
        android:layout_marginRight="14dp"
        android:layout_toLeftOf="@+id/btnFecharApp"
        android:layout_toStartOf="@+id/btnFecharApp"
        android:background="@drawable/reload" />

    <Button
        android:id="@+id/btnConfig"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="19dp"
        android:layout_marginRight="19dp"
        android:layout_marginTop="11dp"
        android:layout_toLeftOf="@+id/btnSobre"
        android:layout_toStartOf="@+id/btnSobre"
        android:background="@drawable/config" />

    <Button
        android:id="@+id/btnFecharApp"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="67dp"
        android:layout_marginRight="67dp"
        android:background="@drawable/fechar"
        android:layout_alignBottom="@+id/btnAtualizar"
        android:layout_alignTop="@+id/btnAtualizar" />

    <Button
        android:id="@+id/btnSobre"
        style="@android:style/Widget.Button"
        android:layout_width="50dp"
        android:layout_height="50dp"

        android:layout_alignTop="@+id/btnConfig"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:layout_toLeftOf="@+id/btnAtualizar"
        android:layout_toStartOf="@+id/btnAtualizar"
        android:background="@drawable/sobre" />

</RelativeLayout>

In the Java code I use the following to configure button parameters:

    RelativeLayout.LayoutParams layoutParams1= (RelativeLayout.LayoutParams) btnSAE.getLayoutParams();

    //alguns comandos para cálculos envolvendo as métrica do display (DisplayMetrics)
    //após os cálculos tenho as variáveis indicando os pixels necessários
    //para ajustar meu layout ao celular do usuário

    layoutParams1.setMargins(l,t,r,b);
    layoutParams1.setMarginEnd(e);
    layoutParams1.setMarginStart(s);
    btnSAE.setLayoutParams(layoutParams1);
    btnSAE.setHeight(h);
    btnSAE.setWidth(w);

But this only adjusts the button and not the Relative Layout in which it is included (the width and height). How do I do?

    
asked by anonymous 31.10.2017 / 16:40

1 answer

1

Normally it is not necessary / advisable to assign absolute values to android:layout_width and android:layout_height of layouts.

The default is to assign match_parent or wrap-content .

However you can change these values via java code in the same way you change in any other view. You only need to give it @id so you can reference it and get it with findViewById() .

    
31.10.2017 / 16:51