android - 将 View 添加到 RelativeLayout 会更改其大小

标签 android android-layout layout

我有一个名为“Dress”的 RelativeLayout,其 LayoutParams 为 WRAP_CONTENT。它里面只有一个 ImageView,所以它和 ImageView 一样大。当我在 OnCreate 中向 RelativeLayout 添加一个 View 时,如下所示:

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

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

我截取了 Dress Layout 的屏幕截图。所以当 photoSorter 像这样在 RootLayout(不是 Dress Layout)中时:

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 添加到 Dress Layout 时:

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

它使 Dress Layout 占据了整个屏幕,因此 Dress Layout 的屏幕截图如下所示:

enter image description here

这是我截屏的代码:

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

这是我的 Activity 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 占据了整个 Activity 。所以在 Dress Layout 中添加任何东西都会让它变得更大。

如何在不让 Dress Layout 占用整个 Activity 大小的情况下将 photoSorter 添加到 Dress Layout?

谢谢。

最佳答案

我通过尝试使用 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。

关于android - 将 View 添加到 RelativeLayout 会更改其大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21278537/

相关文章:

用于 Skype 的安卓 API?

android - 如何更改自定义警报对话框的背景

c# - 创建一个方形边框,内部有图像,宽度和高度取决于统一的网格列

android - 与 xamarin android 一起使用的模拟框架

Android Facebook ..如何获取AccessToken

android - 为 ListView 中的每个元素设置不同的分隔线颜色

android - Scrollview 不适用于 Cardview

android - 附加到底部的 AdView 不允许我在 Recyclerview 中看到最后一项

css - 按钮不进入div?

java - MigLayout 中的单元格