【问题标题】:Android - Make a background image not stretch it's assigned view outAndroid - 使背景图像不拉伸分配给它的视图
【发布时间】:2023-03-20 22:01:01
【问题描述】:

图片万岁!这是将图片分配给背景时的样子,以及没有时的样子。

如果图像大于表格,我非常希望它不要拉伸顶部TableView。我已经包含了一个空的“视图”,以便为表格的背景提供一点额外的空间,正如您在下面的 XML 中看到的那样。试图弄乱 ScaleType,但那是洗礼。

如何让 BG 拉伸以适应布局?我正在尝试优雅地处理“用户 xxx 的屏幕比 WVGA 小”。显然,我缺乏优雅。

<TableLayout
android:id="@+id/widget29"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scaleType="fitXY"
android:background="@drawable/minetownx480"
>
<View
android:id="@+id/blankSpace"
android:layout_width="fill_parent"
android:layout_height="60dip" />

<TableRow android:id="@+id/widget40" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
    <TextView android:id="@+id/moneyText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Money: $$$$$$$$$" />
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"  android:id="@+id/savingsText"  android:gravity="right"  android:text="Savings: $$$$$$$"  android:layout_weight="3" android:textSize="12sp"/>
</TableRow>

<TableRow android:id="@+id/widget41" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
    <TextView android:id="@+id/wagonText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Wagon Space: XXX/XXX" />
    <TextView android:id="@+id/loanText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Loans: $$$$$$$$" android:gravity="right" android:layout_weight="3" android:textSize="12sp"/>
</TableRow>

<TableRow android:id="@+id/widget42" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" >
    <TextView android:id="@+id/daysText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Days Left: XX/XX" />
    <TextView android:id="@+id/armedText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Unarmed!" android:gravity="right" android:layout_weight="3" />
</TableRow>
</TableLayout>

tablelayout 是根 linearLayout 的第一个元素,如果这很重要的话。

编辑:或者,如果您知道如何在分配图像之前测量 tableLayout 的 X 和 Y 空间,我可以在分配图像之前以编程方式对其进行缩放......如果有人知道,这两种解决方案都会很好!

【问题讨论】:

    标签: android background tablelayout stretch


    【解决方案1】:

    我会使用 FrameLayout 并创建两个图层(即在里面添加两个视图)。第一个是 ImageView,第二个是你的桌子。通过这种方式,我不会依赖表格来控制我的图像缩放,但它将独立于 ImageView,然后希望更容易操作它,因为它不是将图像显示为“背景”,而是显示为源。

    【讨论】:

    • 优秀。我使用它实现了它并且它正在工作。我完全不知道 frameLayout 是如何工作的,这是一个很好的解释。