Android 将视图添加到RelativeLayout会更改其大小
我有一个名为“连衣裙”的相对论,带有包装内容的布局参数。它的内部是一个ImageView,因此它的大小与ImageView相同。当我在OnCreate中将另一个视图添加到RelativeLayout时,如下所示:Android 将视图添加到RelativeLayout会更改其大小,android,android-layout,layout,Android,Android Layout,Layout,我有一个名为“连衣裙”的相对论,带有包装内容的布局参数。它的内部是一个ImageView,因此它的大小与ImageView相同。当我在OnCreate中将另一个视图添加到RelativeLayout时,如下所示: photoSorter = new MultitouchImagesView(this.getApplicationContext()); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutPar
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((ViewGroup) findViewById(id.Dress)).addView(photoSorter, params);
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,基本上是整个活动,减去actionbar
我拍了一张服装布局的截图。因此,当photoSorter位于RootLayout(而不是Dress Layout)中时,如下所示:
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addContentView(photoSorter, params);
礼服的布局尺寸正确,我得到了我想要的屏幕截图:
但我需要在服装布局中使用PhotoSorter,这样我就可以将其包含在我的截图中,而不必使用黑色条纹。当我将Photosort添加到衣服布局时,如下所示:
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((ViewGroup) findViewById(id.Dress)).addView(photoSorter, params);
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
((ViewGroup) findViewById(id.Dress)).addView(photoSorter, params);
它使服装布局占据整个屏幕,因此服装布局的屏幕截图如下所示:
photoSorter = new MultitouchImagesView(this.getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addContentView(photoSorter, params);
下面是我截图的代码:
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.crapper.CropImageView添加到DressLayout时,它也使DressLayout占据了整个活动。因此,在服装布局中添加任何东西都会使其变得更大
如何将photoSorter添加到服装布局中,而不使服装布局占据整个活动大小
谢谢。下面的方法应该有效
<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" />
我用XML进行了尝试。关键是将裙子与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添加到DressBig,然后拍摄DressBig的屏幕截图。谢谢:)我将解释一下我的应用程序。改变图像比例或裁剪是不可能的。这不会发生,因为我不想让图像与屏幕大小相匹配。它变为屏幕的宽度,然后高度在它停止的地方停止。因此,它大约占屏幕大小的90%。然后,我只拍摄ImageView和PhotoSorter的快照。服装布局是拍摄同样包含PhotoSorter视图的ImageView大小的快照所必需的。不是活动大小的屏幕截图。我会试一试的。没有这样的运气,但是谢谢你试一试。match_parent将实现您认为我想要的(全屏图像)。尝试我在编辑的答案中所做的,fitXY应按照您所述调整图像大小,然后衣服视图可以包含背景并确保没有黑线我刚刚意识到我不理解您的问题,我想我现在明白你想要什么了,但是如果你能附上一张完成布局的草图,这样我就可以确定了,那么我可以帮你,谢谢。我拍摄的服装布局截图将与我发布的头像一样。但这并不是因为图像与屏幕大小有关。因为屏幕的其余部分是从截图中剪掉的。这是通过使衣服布局与ImageView的大小相同(通过使其包裹内容)来实现的。我添加的额外视图(photoSorter)也为屏幕截图中显示的活动添加了额外的图像,这就是为什么我需要在服装布局中添加photoSorter,但当我在OnCreate中添加它时,它似乎会使服装布局全屏显示。