【问题标题】:Android: How to resize BackgroundDrawable for a Linear Layout dynamicallyAndroid:如何动态调整线性布局的 BackgroundDrawable 大小
【发布时间】:2012-07-07 22:54:29
【问题描述】:

我必须为 LinearLayout 设置 BackgorundDrawbale。 BackgroundDrawable 具有比 LinearLayout 更大的高度并忽略 XML 布局中定义的高度(除了我不想使用绝对高度而是“wrap_content”) - 因此增加了布局的高度。有没有办法动态缩放 Drawable 而不增加高度? - 或者我是否必须在 Photoshop 中将 Drawable 裁剪到正确的高度? - 一定有别的办法。

我已经在 StackOverflow 上解决了使用 RelativeLayout 的类似问题。但我没有得到想要的结果。

我的布局如下所示,它在 ListView 中用于第一行。布局应该在图像下方结束 - 但不是因为 Drawable。

目前我正在使用以下 XML 布局。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/black">

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/foto_detail_bg_cell_0"
        >

    <ImageView
            android:id="@+id/icon"
            android:layout_width="100px"
            android:layout_height="100px"
            android:layout_marginRight="6dip"
            android:layout_marginTop="6dip"
            android:layout_marginBottom="6dip"
            android:src="@drawable/icon"

            />

    <LinearLayout
            android:orientation="vertical"
            android:paddingTop="5dip"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="wrap_content"

            >
        <TextView
                android:id="@+id/toptext"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:ellipsize="marquee"
                android:layout_weight="1"
                android:paddingLeft="10dip"
                android:gravity="center_vertical"
                android:textStyle="bold"
                android:textSize="14dip"
                />
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:maxLines="3"
                android:scrollHorizontally="false"
                android:layout_weight="1"
                android:paddingLeft="10dip"
                android:id="@+id/bottomtext1"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:textSize="14dip"

                />

        <TextView
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:paddingLeft="25dip"
                android:id="@+id/bottomtext2"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:textSize="14dip"

                />
    </LinearLayout>

</LinearLayout>

我会很高兴得到一些提示。

【问题讨论】:

    标签: android android-layout background-image android-linearlayout android-relativelayout


    【解决方案1】:

    最后我使背景图像小于布局的高度。现在它会自动缩放(拉伸)到正确的大小。

    【讨论】:

    • 是的,我也没有找到任何替代方案
    【解决方案2】:

    使用单一的相对布局,并使用环绕内容的高度和宽度进行固定。对我来说工作正常的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:clickable="true"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:clickable="true"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:layout_marginTop="10dip"/>
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="#000000"/>
    
        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"/>
    
    </RelativeLayout>
    

    【讨论】:

    • 我应该把android:background="@drawable/foto_detail_bg_cell_0"这一行放在哪里?
    猜你喜欢
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2012-11-25
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多