【问题标题】:ImageView match_parent different for Jelly Bean and FroyoJelly Bean 和 Froyo 的 ImageView match_parent 不同
【发布时间】:2013-04-19 20:25:56
【问题描述】:

我有一个简单的布局,但遇到了 Jelly Bean 和 Froyo 之间的差异。在 Jelly Bean 上,我有 ImageView 匹配其父级 (FrameLayout) 的高度。但在 Froyo 上却不是。请看一下布局结构:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/profile_data_background_im"
            android:scaleType="fitXY"/>

        <LinearLayout
            android:id="@+id/ll"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test text"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test text"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test text"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test text"/>

        </LinearLayout>

        </FrameLayout>

</LinearLayout>

所以我在 Jelly Bean 上拥有的东西(正如预期的那样):

这是我在 Froyo 上的内容:

请告知如何让ImageView 填充其父级,就像它用于 Jelly Bean 一样。非常感谢。

【问题讨论】:

  • 首先,您的屏幕截图是 Eclipse 预览的,这并不是特别相关。使用模拟器或设备进行测试。其次,当你运行 Hierarchy View 来查看你的ImageView 的大小时,你学到了什么?
  • 这是一个错误; see here, also for a workaround
  • 马克,我已经在真实设备上进行了测试,我刚刚从 IDE 截屏。我没有检查层次结构视图,我会这样做。
  • 为什么还有 FrameLayout?你已经有一个 LinearLayout...
  • @WarrenFaith 这只是更复杂布局的一部分,我需要它。

标签: android android-4.2-jelly-bean android-2.2-froyo


【解决方案1】:

我最近才遇到同样的现象。在查看 Android 源代码和子类化 View 以查看 FrameLayout 将传递给其子级的值之后,很明显这是一个错误。

请参阅此处了解完整的 description 和 workaround。

元注释:感谢 StackOverflow 审阅者投票支持将我的原始答案转换为评论,然后反对将此问题标记为重复。现在这就是你得到的。 ;)

【讨论】:

    【解决方案2】:

    将您的FragmentLayout 的layout_height 设置为match_parent。

    您的 XML 代码应如下所示

    <FrameLayout
            android:id="@+id/fl"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    

    【讨论】:

    • 不,我需要它是“wrap_content”,因为这只是更复杂布局的一部分。
    • 这会导致整个屏幕的背景被拉伸。
    • 问题是我不需要将它拉伸到整个屏幕而是部分。
    【解决方案3】:

    为什么不把profile_data_background_imDrawable 作为@+id/llLinearLayout 的背景?无论如何,奇怪的布局组合。

    【讨论】:

    • 因为我在这里使用更复杂的解决方案,第三方库有一个带有圆角和平铺背景的 ImageView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多