How to wrap content views rather than background drawable?

前端 未结 5 1377
攒了一身酷
攒了一身酷 2020-12-06 04:48

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?



        
相关标签:
5条回答
  • 2020-12-06 04:56

    I believe the best solution here is to set android:clipToPadding="true". What this does is excludes the padding for the main layout and wraps your layout to its children.

    0 讨论(0)
  • 2020-12-06 04:57

    You can create a FrameLayout and put an ImageView and your LinearLayout there. So you'll be able to configure the layout of your background image.

    0 讨论(0)
  • 2020-12-06 05:12

    If your image lends itself to being converted to a scalable 9-patch image, then doing that would cause the background to scale around the TextView.

    0 讨论(0)
  • 2020-12-06 05:13

    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.

    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>
    

    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.

    0 讨论(0)
  • 2020-12-06 05:16

    This happens because the Android view calculates its minimum size based on its background drawable size.

    Check my answer here in this another post which covers the same problem which will help you to achieve your layout configuration.

    0 讨论(0)
提交回复
热议问题