简体   繁体   中英

Keeping relative layout fixed

I would like to know how can I keep my relative layout fixed. I have a relative layout inside that I have an image view. I am also adding custom views to my relative layout. But when I move my custom views the relative layout height increases.

I want the custom view to get cut if it goes out of the bound of my relative layout.

Below is my relative layout

<RelativeLayout 
    android:id="@+id/relativelayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@android:color/holo_blue_light" >

    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="@string/content_description"
        android:src="@drawable/img" />    

</RelativeLayout>

Below is the code where I am moving my custom view

relativeLayoutParams.leftMargin += dx;
relativeLayoutParams.topMargin += dy;
v.setLayoutParams(relativeLayoutParams);                        
v.invalidate();

If the RelativeLayout is the main view in the layout, move it into a ScrollView which fills the entire screen. Here is how you can do it

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

    <RelativeLayout 
        android:id="@+id/relativelayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light" >

        <ImageView 
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:contentDescription="@string/content_description"
            android:src="@drawable/img" />    

    </RelativeLayout>
</ScrollView>

If the RelativeLayout is not the main view, then you will have to specify some fixed height to it as using wrap_content for layout_height will make the RelativeLayout wrap its contents and it will increase its height accordingly.

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