将视图添加到RelativeLayout会更改其大小

时间:2014-01-22 09:11:00

标签: android android-layout layout

我有一个名为“Dress”的RelativeLayout,其中包含WRAP_CONTENT的LayoutParams。其中的所有内容都是ImageView,因此它与ImageView的大小相同。当我在OnCreate中向RelativeLayout添加一个视图时,如下所示:

photoSorter = new MultitouchImagesView(this.getApplicationContext());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ((ViewGroup) findViewById(id.Dress)).addView(photoSorter, params);

RelativeLayout名为Dress改变大小,占据整个RootView,基本上是整个活动,减去动作栏。

我拍摄了连衣裙布局的截图。所以当photoSorter在RootLayout(不是连衣裙布局)中时,就像这样:

photoSorter = new MultitouchImagesView(this.getApplicationContext());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addContentView(photoSorter, params);

布局连衣裙尺寸正确,我得到了这个我想要的截图:

enter image description here

但我需要在Dress Layout中使用PhotoSorter,这样我就可以将它包含在我的截图中,而不是黑色条带。当我将photoSortr添加到连衣裙布局时,如下所示:

photoSorter = new MultitouchImagesView(this.getApplicationContext());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            ((ViewGroup) findViewById(id.Dress)).addView(photoSorter, params);

它使连衣裙布局占据整个屏幕,因此连衣裙布局的屏幕截图如下所示:

enter image description here

以下是我拍摄屏幕截图的代码:

public Bitmap takeScreenshot() {
        View v = findViewById(R.id.Dress);
        v.setDrawingCacheEnabled(true);
        return v.getDrawingCache();
    }

以下是我的活动XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:longClickable="true"
    android:onClick="deleteImage"
    android:background="@android:color/transparent" 
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/Dress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" >

    <ImageView
        android:id="@+id/imageViewDress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:background="#00CED1"
        android:padding="10dp"
        android:scaleType="centerInside" />

    </RelativeLayout>

    <com.edmodo.cropper.CropImageView 
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/CropImageView"
    android:background="@android:color/transparent" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

此外,当我将com.edmodo.cropper.CropImageView添加到DressLayout时,它也使得Dress Layout也会占用整个活动。因此,在Dress Layout中添加任何东西都会让它更大。

如何在不使连衣裙布局占用整个活动尺寸的情况下将photoSorter添加到连衣裙布局?

感谢。

2 个答案:

答案 0 :(得分:1)

我通过在XML中尝试解决问题。关键是将Dress与imageViewDress对齐,我可以将其他边对齐得更彻底,而不仅仅是底部。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:longClickable="true"
        android:background="@android:color/transparent" 
        tools:context=".DressingRoomActivity" >

        <RelativeLayout 
            android:id="@+id/DressBig"
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

            <ImageView
            android:id="@+id/imageViewDress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:scaleType="centerInside" />

             <RelativeLayout
            android:id="@+id/Dress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:layout_alignBottom="@+id/imageViewDress"
            android:layout_centerInParent="true" >
             </RelativeLayout>

        </RelativeLayout>

        <com.edmodo.cropper.CropImageView 
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/CropImageView"
        android:background="@android:color/transparent" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>

这个XML,然后以编程方式将photoSorter添加到Dress中,然后截取DressBig的截图。

答案 1 :(得分:0)

以下内容应该有效

<RelativeLayout android:id="@+id/Dress"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="...">

<ImageView
    android:id="@+id/imageViewDress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
    android:scaleType="fitXY" />

编辑:

您可以将以下规则添加到您的参数中,以便在您想要的时候将其放在其他内容上。这是我尝试找出你所要求的第三次尝试,如果它不是你需要的,你必须自己弄清楚,因为你是懒惰的,不会做草图试图解释显然你想要它如何布局

params.addRule(RelativeLayout.CENTER_IN_PARENT);
相关问题