【问题标题】:fill_parent not stretching background imagefill_parent 不拉伸背景图像
【发布时间】:2012-04-26 08:46:21
【问题描述】:

我在这里疯了。我发誓我以前做过一千次这项工作,现在好像没有任何行为正常。我必须做一些愚蠢的事情。几天前,我遇到了列表视图项的背景问题,没有填充父项以延伸到列表视图项的高度。我终于放弃了,只是硬编码了一个高度来解决这个问题。现在我只是想设置整个活动的背景并让它填充它的父项,以便图像覆盖整个屏幕。此活动位于选项卡内。这是标签控制器的代码和我遇到问题的活动。

标签布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@android:id/tabcontent"            
            android:layout_width="fill_parent"            
            android:layout_height="fill_parent">

        </FrameLayout>
</LinearLayout>
</TabHost>

麻烦的活动:

<?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="@drawable/background">

<RelativeLayout
    android:id="@+id/localHeader"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="@drawable/header2">

    <Button
        android:id="@+id/changeCityButton"
        android:layout_width="80dip"
        android:layout_height="30dip"
        android:layout_marginRight="10dip"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:onClick="buttonChangeOnClickListener"
        android:text="Change"
        android:textColor="#FFFFFF"
        android:background="@drawable/local_deal_redeem_button" />

    <TextView
        android:id="@+id/cityName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/changeCityButton"
        android:textSize="20dip"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:shadowColor="#FF000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="1"/>

</RelativeLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/localHeader"
    android:fastScrollEnabled="true"
    android:cacheColorHint="#00000000"
    android:divider="@null"
    android:dividerHeight="0px"/>

</RelativeLayout>

这个问题似乎只发生在新的 Bionic 上。 “@drawable/background”只会拉伸到列表视图的长度。如果列表视图中没有项目,它将不会显示任何内容。我什至尝试将其从布局中剥离出来,使其只是一个图像视图,但这也不起作用。就好像我在某处缺少一个 wrap_content 标记。通常我只会说这是仿生的问题,但看到我在 ListView 背景中遇到的这些问题,我想我在某个地方搞砸了。请帮忙!!

也在我的清单中:

<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:resizeable="true" android:anyDensity="true"></supports-screens>

但我已经尝试删除它,无论我在该行中输入什么值,它都不会在我的应用程序中执行任何操作。

编辑:我刚刚意识到,如果我将图像放入“fill_parent”滚动视图,背景会相应地拉伸。但是,由于屏幕包含一个 ListView,我不能走这条路。这里有些古怪。

编辑 2: 我刚刚删除了列表视图,它运行良好。它在某种程度上与列表视图相关。

编辑 3: 我刚刚通过将 ListView 高度设置为“wrap_content”解决了这个问题。我不知道为什么“fill_parent”会在此设备上导致此行为,但它已修复,所以现在一切都很好。再过 6 个小时我都无法自我回答,所以如果有人想将此作为答案,我会选择它作为正确答案。谢谢大家!

【问题讨论】:

  • 你试过 android:scaleType="fitXY"
  • 它必须与在标签内相关。我刚刚尝试了一个独立的活动,它工作正常。

标签: android android-layout tabs fill-parent


【解决方案1】:

我通过将 ListView 高度设置为 wrap_content 解决了这个问题。我不知道为什么 fill_parent 会在这部手机上导致这种行为,但它现在可以正常运行,所以一切都很好。谢谢大家!

【讨论】:

    【解决方案2】:

    用片段试试这个逻辑,因为 tab 现在已被弃用。这是我的观点,如果你尝试一下,它也可能是错误的。

    【讨论】:

    • 现在有能力重新设计应用程序。这是构建发布之前要解决的最后一个问题。
    • 但我认为你的问题是现在宣布弃用的标签活动
    • 问题实际上是ListView。仅仅因为它被宣布弃用,它不会导致它突然停止在以前的设备上正常运行。我当然感谢您的意见,并将在未来的构建中实施 Fragments。
    【解决方案3】:

    我只是在研究这个问题,发现了这个:

    http://community.developer.motorola.com/t5/MOTODEV-Blog/Why-Does-My-ListView-Look-Different/ba-p/17462

    基本上,Bionic 上的默认主题在 ListView 上设置了 android:overScrollFooter。将其设置为 @null 将提供其他设备表现出的行为。

    【讨论】: