【问题标题】:How to wrap content views rather than background drawable?如何包装内容视图而不是背景可绘制对象?
【发布时间】:2010-11-30 09:21:17
【问题描述】:

如何让 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>

线性布局变得与背景图像大小相同。 如何让我的线性布局假定与 textview 相同的大小?

【问题讨论】:

    标签: android background


    【解决方案1】:

    好的,所以这个帖子有点老了,但我有一个解决方案,总有一天有人会觉得有用。我认为 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 不需要任何相对定位,因为假定为左上角。

    【讨论】:

    • 经过数小时的搜索,这是唯一对我有用的解决方案。谢谢。
    • 根据您对背景的意图,使用scaleType="centerCrop" 而不是fitXY 可能很重要。 fitXY 将拉伸/收缩图像,使其与生成的 imageView 的大小完全匹配,无论多高或多瘦。 centerCrop 将缩小图像以使其适合 imageview 尺寸,但会保存纵横比,如果这对您很重要的话。
    • 最好使用android:scaleType="centerCrop"
    【解决方案2】:

    您可以创建一个FrameLayout 并将ImageView 和您的LinearLayout 放在那里。这样您就可以配置背景图像的布局。

    【讨论】:

    • 在那个 ImageView 中,我将宽度和高度都设置为 match_parent,将 Tpye 缩放为 fitXY 并启用了 adjustViewBounds。有效!
    【解决方案3】:

    这是因为 Android 视图根据其背景可绘制尺寸计算其最小尺寸。

    在另一篇文章中查看my answer here,它涵盖了相同的问题,这将帮助您实现布局配置。

    【讨论】:

      【解决方案4】:

      如果您的图像适合转换为可缩放的 9-patch 图像,那么这样做会导致背景围绕 TextView 缩放。

      【讨论】:

      • 是的,很遗憾我的图片不平铺,也不适合 9-patch。
      • 我不确定这是否正确...在使用 9 补丁时,我仍然看到同样的问题发生。 9 个补丁会扩大,但似乎不会缩小。
      【解决方案5】:

      我相信这里最好的解决方案是设置 android:clipToPadding="true"。这样做是排除主布局的填充并将您的布局包装到其子级。

      【讨论】:

        猜你喜欢
        • 2014-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        相关资源
        最近更新 更多