如何让LinearLayout (或任何其他ViewGroup)假设它的子视图的大小,而不是假设背景图像的大小?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/large_image300x300pix">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world!"/>
</LinearLayout>线性布局变得与背景图像的大小相同。如何才能使我的线性布局与文本视图的大小相同?
发布于 2013-10-16 21:52:19
好吧,这个帖子有点老了,但我有一个解决方案,也许有一天有人会发现它很有用。我认为Android在缩小大图像方面有问题,所以LinearLayout的大小最终会被背景可绘制的位图增大,而ImageView最终会强制增大父容器的大小。
除非您使用相对布局。您可以将ImageView设置为相对于LinearLayout的位置,即使ImageView位于父级中的布局之后。我的解决方案看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@drawable/activation_popup"
android:scaleType="fitXY"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignTop="@+id/activation_layout"
android:layout_alignBottom="@id/activation_layout"
android:layout_alignLeft="@id/activation_layout"
android:layout_alignRight="@id/activation_layout"
android:contentDescription="@string/act_code_label" />
<LinearLayout
android:id="@id/activation_layout"
android:clipToPadding="true"
android:padding="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- LinearLayout wraps a bunch of smallish views here -->
</LinearLayout>
</RelativeLayout>我在不同的显示尺寸和操作系统版本上尝试了一下,看起来效果很好。请注意,LinearLayout中的填充是为背景图像图形中的阴影边框留出空间的技巧。LinearLayout不需要任何相对定位,因为假设了左上角。
发布于 2010-12-01 04:37:05
您可以创建一个FrameLayout并将ImageView和您的LinearLayout放在那里。这样你就可以配置背景图像的布局了。
发布于 2015-07-08 19:28:58
这是因为Android视图根据其背景可绘制大小计算其最小大小。
在另一篇文章中查看my answer here,这篇文章涵盖了相同的问题,这将帮助你实现布局配置。
https://stackoverflow.com/questions/4312133
复制相似问题