保持相对布局固定

时间:2014-01-16 04:57:48

标签: android android-layout relativelayout

我想知道如何保持相对布局的固定。我有一个相对布局,我有一个图像视图。我也在为我的相对布局添加自定义视图。但是当我移动自定义视图时,相对布局高度会增加。

如果自定义视图超出我的相对布局范围,我希望自定义视图能够被剪切。

以下是我的相对布局

<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>

以下是我移动自定义视图的代码

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

1 个答案:

答案 0 :(得分:0)

如果RelativeLayout是布局中的主视图,请将其移动到填充整个屏幕的ScrollView。这是你如何做到的

<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>

如果RelativeLayout不是主视图,那么您必须为其指定一些固定的height,因为使用wrap_content layout_height会使RelativeLayout包裹它的内容,它会相应地增加它的高度。