Pergunta

I want to change te height of a Relative layout. I want to change it from 70dp to 80dp when you push on a button. I checked if the button works with visibility change and is does work.

I have searched for answers see Link Changing relative layout programmatically

in that answers this code is mentioned:

 RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
 rl.getLayoutParams().height = 100;
 rl.getLayoutParams().width = 100;

I tried the above code and it wont work, Nothing happens. see my code:

  public void next (View view){
    if (view == mnext){

                RelativeLayout.LayoutParams paramsT1 = (RelativeLayout.LayoutParams)mT1layout.getLayoutParams();
                // Changes the height and width to the specified *pixels*
                paramsT1.height = 300;                

      }
   }

This is my .XML The Relativelayout is placed in a linearlayout.

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="70dp"
        android:background="#314ebd"
        android:id="@+id/LayoutT1score"
        android:layout_toLeftOf="@+id/LayoutT2score">

        <TextView
            android:id="@+id/player1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 1"
            android:id="@+id/lblTeam1"
            android:layout_above="@+id/player1"
            android:layout_alignLeft="@+id/player1"
            android:layout_alignStart="@+id/player1"
            android:textSize="24dp"
            android:textColor="#ffffff" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="60dp"
        android:id="@+id/LayoutT2score"
        android:background="#fbff62"
        >

        <TextView
            android:id="@+id/player2"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="0dp"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 2"
            android:id="@+id/lblTeam2"
            android:textSize="24dp"
            android:textColor="#000000"
            android:layout_above="@+id/player2"
            android:layout_alignLeft="@+id/player2"
            android:layout_alignStart="@+id/player2" />


    </RelativeLayout>
  </LinearLayout>
Foi útil?

Solução

You forgot to call mT1layout.setLayoutParams(paramsT1) or mT1layout.invalidate() after changing the height.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top