Adding a View to RelativeLayout changes its size

久未见 提交于 2020-01-15 04:08:20

问题


I have a RelativeLayout called "Dress" with LayoutParams of WRAP_CONTENT. All that is inside it is an ImageView, so it is the same size as the ImageView. When I add one more view to the RelativeLayout in OnCreate, like this:

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

The RelativeLayout called Dress changes size, to take up the whole RootView, basically the whole activity, minus the actionbar.

I take a screenshot of the Dress Layout. So when photoSorter is in the RootLayout (NOT the Dress Layout) like this:

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

Layout Dress is correct size and I get this screenshot which is what I want:

But I need PhotoSorter in the Dress Layout so that I can include it in my screenshot and not have the black strips. When I add the photoSortr to the Dress Layout like this:

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

It makes the Dress Layout take up the whole screen so the screenshot of the Dress Layout looks like this:

Here is my code to take a screenshot:

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

Here is my XML for the activity:

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

Also When I added com.edmodo.cropper.CropImageView to the DressLayout, it made the Dress Layout take up the whole activity as well. So adding anything to the Dress Layout is making it bigger.

How can I add photoSorter to the Dress Layout without making Dress Layout take up the whole activity size?

Thanks.


回答1:


I managed by trying things out in XML. The key is to align Dress with imageViewDress, I could align the other sides to be more thorough, not just bottom.

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

This XML, then add photoSorter to Dress programmatically, then take a screenshot of DressBig.




回答2:


The following should work

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

EDIT:

You can add the below rule to your params so it's laid out over the other content if that's what you want. This is my third attempt at trying to figure out what you're asking for so if it's not what you need, you'll have to figure it out yourself seeing as you are being lazy and won't do a sketch to try and explain clearly how you want it laid out

params.addRule(RelativeLayout.CENTER_IN_PARENT);


来源:https://stackoverflow.com/questions/21278537/adding-a-view-to-relativelayout-changes-its-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!