Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何包装内容视图而不是后台绘图?_Android_Background - Fatal编程技术网

Android 如何包装内容视图而不是后台绘图?

Android 如何包装内容视图而不是后台绘图?,android,background,Android,Background,如何使LinearLayout(或任何其他视图组)假定其子视图的大小,而不是假定背景图像的大小 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/large_ima

如何使LinearLayout(或任何其他视图组)假定其子视图的大小,而不是假定背景图像的大小

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

线性布局与背景图像的大小相同。
如何使我的线性布局与textview的大小相同?

如果您的图像适合转换为可缩放的9补丁图像,然后这样做会导致背景在文本视图周围缩放。

您可以创建一个
框架布局
,并将
图像视图
和您的
线性布局
放在那里。因此,您可以配置背景图像的布局。

我认为最好的解决方案是设置android:clipToPadding=“true”。这样做的目的是排除主布局的填充,并将布局包装到其子级

好的,这个线程有点旧了,但我有一个解决方案,有人可能会觉得有用。我认为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中的填充是为背景图像图形中的阴影边框留出空间的一种技巧。线性布局不需要任何相对定位,因为假定为左上角。

之所以出现这种情况,是因为Android视图根据其背景可绘制大小计算其最小大小


请参阅另一篇文章,其中介绍了同样的问题,这将帮助您实现布局配置。

是的,不幸的是,我的图像不平铺,不适合9-patch。我不确定这是否正确。。。在使用9-patches时,我仍然看到同样的问题发生。9面片将展开,但似乎不会收缩。在该图像视图中,我将宽度和高度设置为匹配父对象,将Tpye缩放为fitXY并启用adjustViewBounds。作品经过数小时的搜索,这是唯一对我有效的解决方案。谢谢。不过,根据您的背景意图,使用
scaleType=“centerCrop”
而不是
fitXY
可能很重要。fitXY将拉伸/收缩图像,使其与生成的imageView的大小完全匹配,而不管图像有多高或多瘦
centerCrop
将缩小图像的比例,使其适合imageview尺寸,但如果这对您很重要,将保存纵横比。最好使用
android:scaleType=“centerCrop”