简体   繁体   中英

How to change layout height programmatically in Android

I try to change RelativeLayout height programmatically. I try to explain my problem: In my activity.xml file, I have header layout(top position) and I also have one layout in botton position. With a click, I try to change botton position layout's height(full screen - Top layout height).

Here is activity.xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:id="@+id/movies_title_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/headerimage" >

    <ImageView
        android:id="@+id/gomoviewlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:background="@drawable/slide_menu_image" />
</RelativeLayout>

<ImageView
    android:id="@+id/play_trailer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/play_trailer" />

<RelativeLayout
    android:id="@+id/popaplayout"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/movie_info_shadow" >

    <TextView
        android:id="@+id/movies_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="14dp"
        android:text="TextView"
        android:textColor="#ffffff"
        android:textSize="22dp" />

    <TextView
        android:id="@+id/category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/movies_title"
        android:layout_below="@+id/movies_title"
        android:layout_marginTop="16dp"
        android:text="TextView"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/descraption"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/category"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="5dp"
        android:text="TextView"
        android:textColor="#ffffff"
        android:textSize="20dp" />
</RelativeLayout>

Here is Java code:

popaplayout.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT-movies_header_layout.getWidth()  );

            popaplayout.setLayoutParams(layout_description);

        }
    });

popaplayout is the layout witch I try to change it's height.

How can I solve my problem?

Add this to your onClickListener:

((RelativeLayout.LayoutParams)popaplayout.getLayoutParams()).addRule(RelativeLayout.BELOW, R.id.movies_title_layout);

This will make the view's top the same as the header's bottom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM