将视图添加到RelativeLayout会改变它的大小

2023-12-19

我有一个名为“Dress”的相对布局,其 LayoutParams 为 WRAP_CONTENT。它里面的所有内容都是一个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);

称为 Dress 的相对布局会更改大小,以占据整个 RootView,基本上是整个 Activity,减去操作栏。

我截取了服装布局的屏幕截图。因此,当 photoSorter 在 RootLayout (不是 Dress Layout)中时,如下所示:

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

布局连衣裙尺寸正确,我得到的屏幕截图正是我想要的:

但我需要在 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 的屏幕截图如下所示:

这是我的截图代码:

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 时,它使得 DressLayout 也占据了整个活动。因此,在服装布局中添加任何内容都会使其变得更大。

如何将 photoSorter 添加到 Dress Layout 而不使 Dress Layout 占用整个活动大小?

Thanks.


我通过在 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 的屏幕截图。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将视图添加到RelativeLayout会改变它的大小 的相关文章

  • 如何使用约束布局来模拟加权线性布局

    我们如何在约束布局中像在 LinearLayout 中一样平等地节省空间 例如 如果下面的布局是带有约束的 那么它会变成什么样子
  • 在 Samsung Galaxy Tab 2.3.3 android 上禁用列表视图过度滚动

    我需要在列表视图中完全禁用过度滚动 以便我可以实现自己的过度滚动功能 查看核心列表视图类时似乎很简单 只需设置overscroll模式设置为 OVERSCROLL NEVER 这在我的三星 Galaxy s2 上表现良好 但不起作用Gala
  • 如何使用appium和java在Android中向下滚动以单击元素?

    我想知道如何使用appium和java向下滚动以单击Android中的元素 我里面有一个元素列表 android support v7 widget RecyclerView由于它有10多个元素 所以我们需要滑动屏幕才能看到下面的元素 每个
  • 没有标题和边框的自定义对话框

    基于这里的代码 http developer android com guide topics ui dialogs html CustomDialog http developer android com guide topics ui
  • Android应用程序:如何读取“设置”下的字体大小?

    我正在开发一个 Android 应用程序 我想阅读下面的字体大小设置 gt 显示 gt 字体大小 因此 如果用户将其字体大小设置为小 大 巨大 我想将其自动应用于我的所有文本 我使用的是三星 Galaxy Nexus Android 4 1
  • GCM 推送通知导致 502 Bad Gateway

    我有一个 Net Windows 服务 通过以下方式发送 GCM 推送通知https android googleapis com gcm send https android googleapis com gcm send 该服务以 10
  • Android 中的发布与调试构建差异

    全部 我们面临一个奇怪的问题 我们的应用程序在调试构建变体中运行良好 但它无法在发布版本变体上正确执行 甚至奇怪的是 如果我们将发布构建变体的 debuggable 设置为 true 它就可以正常工作 Proguard 在这两个变体中都被禁
  • 是否可以直接在 string.xml 中设置字符串的颜色?

    我的意思是
  • Android 上为什么会出现线程泄漏?

    我在 Android 应用程序中注意到 每次退出主屏幕时 堆大小 泄漏 都会增加 ByteArrayOutputStream 的量 我能做到的最好的办法就是添加 this mByteArrayOutputStream null 在 的最后r
  • 传递 Intent 值并从 SQLite 数据库检索数据

    我在android中编写了一个类似于联系人的应用程序 我的列表中有一些项目 当我单击它们时 我从数据库获取信息并显示在另一个视图中 但是当我单击列表视图中的其中一个名称时 我会看到一个空屏幕 模拟器变成空白 请帮忙 我是 Android 编
  • 限制用户在 Mapview 上可以到达的区域

    我正在使用地图视图的定制版本 OSMDroid 版本 我在其中使用自定义图块 我只希望用户能够查看我拥有自定义图块的区域 有没有办法设置边界纬度 以便当他们平移地图时不会超出这些边界 Update 我知道这是一个老问题 但 osmdroid
  • 数据更改后 ListView 未更新

    我正在使用两个ListViews List1 有数据 List2 为空 用户可以输入名称作为输入 如果 List1 包含名称 则添加Button按下后 标记应减 1 并且ListViews应该更新 Example 如果 List1 有 10
  • 发送电子邮件到多个地址 Android

    我想选择多个电子邮件地址 然后向所有这些地址发送电子邮件 我的代码如下 emailIntent putExtra android content Intent EXTRA EMAIL new String listofemailaddres
  • BillingClient 始终返回 SERVICE_DISCONNECTED

    所以我有一个计费客户端 我用它实例化 billingClient BillingClient newBuilder this setListener this build 然后我打电话 billingClient startConnecti
  • 使用适用于 Android 的 asmack XMPP 库时无法从消息标签读取自定义属性?

    这是我想要接收的从 XMPP 服务器端发送的 XML 以便我可以将其发送到我的 XML 解析器
  • Android 前台服务通知未显示

    我正在尝试启动前台服务 我收到服务确实启动的通知 但通知总是被抑制 我仔细检查了应用程序是否允许在我的设备上的应用程序信息中显示通知 这是我的代码 private void showNotification Intent notificat
  • 融合位置有时会停止

    我一直在开发一个跟踪用户位置的应用程序 为此 我使用了谷歌播放服务位置模块 又名融合位置 总而言之 一切正常 但有时 完全随机 我根本不再收到来自谷歌位置服务的位置更新 我的意思是 我的应用程序工作正常 但没有位置更新 不仅如此 如果我启动
  • 语音识别器在第一个结果后听不到声音

    我在Android中使用SpeechRecognizer和RecognizerIntent来实现语音识别 我的目标是在语音识别器在屏幕上显示结果后重新开始收听语音 为此 我使用以下代码 问题是 第一次运行良好并显示结果 但在第二次开始监听
  • Android:“无法初始化可视化引擎”

    我三天前开始为 Android 编程 今天我想使用 android Api 中的一些类来做一些更困难的事情 我找到了 Visualizer 类 第一次尝试时遇到了问题 我在不同论坛上读到很多帖子 人们有同样的问题 无法初始化可视化引擎 我将
  • SQLDroid 和加密数据库

    我的应用程序有一个已经存在的数据库 已经存在 是指我不在我的应用程序中创建数据库 我只是连接到它并读取数据 为了设置连接 我使用 SQLDroid 现在我想知道是否有可能使用 SQLDroid 加密我的数据库 否则我的数据库未加密 任何人都

随机推荐