如何包装内容视图而不是背景可绘制对象? [英] How to wrap content views rather than background drawable?

查看:11
本文介绍了如何包装内容视图而不是背景可绘制对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 LinearLayout(或任何其他 ViewGroup)假设其子视图的大小而不是假设背景图像的大小?

How can I get a LinearLayout (or any other ViewGroup) to assume the size of it's child views rather than assuming the size of the background image?

<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 相同的大小?

The linear layout becomes the same size as the background image. How can I get my linear layout to assume the same size as the textview?

推荐答案

好的,所以这个线程有点老了,但我有一个解决方案,总有一天有人会觉得有用.我认为 Android 在缩小大图像时存在问题,因此 LinearLayout 的大小最终会受到背景可绘制位图的影响,而 ImageView 最终会强制增大父容器的大小.

OK, so this thread is a little old, but I have a solution that someone might someday find useful. I think Android has problems scaling large images down, so the LinearLayout size ends up getting bumped by the background drawable bitmap, and the ImageView ends up forcing up the size of the parent container.

除非您使用相对布局.您可以使 ImageView 相对于 LinearLayout 的位置,即使 ImageView 在父布局的后面.我的解决方案如下所示:

Unless you use a relative layout. You can make the ImageView relative to the position of the LinearLayout, even when the ImageView is behind the layout in the parent. My solution looks something like this:

<?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 不需要任何相对定位,因为假定为左上角.

I tried this on a top of display sizes and OS versions, seems to work great. Note that the padding in the LinearLayout is a trick to make space for a shadow border in the background image graphic. The LinearLayout doesn't need any relative positioning because top left is assumed.

这篇关于如何包装内容视图而不是背景可绘制对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆