【问题标题】:Android ImageView does not match_parent inside FrameLayout below GingerBreadAndroid ImageView 在 GingerBread 下面的 FrameLayout 内不匹配_parent
【发布时间】:2012-09-15 05:23:26
【问题描述】:

我正在尝试为聊天室构建对话泡泡。每个对话气泡都有附加的 xml 布局。

我通过将 textView 设置为 wrap_content、将语音气泡 imageView 设置为 ma​​tch_parentframe_layoutwrap_content。这样文本后面的 speech_bubble 图像会根据气泡中的文本量进行缩放。

我将 textView 上的宽度设置为与父级匹配,并将高度设置为 wrap_content,出于某种原因,它完美地工作,符合 Ice Cream Sandwich 的要求(Android 4.1) . 然而,在 Gingerbread 中,内部 imageView 语音气泡不会缩放到 match_parent,因此文本可以整齐地放入气泡内。 在 Gingerbread 中,内部的 imageView 始终保持相同的大小,无论文本和 文本溢出气泡外。 尽管 frameLayout 扩展为 wrap_content,但 imageView 并没有像应有的那样 ma​​tch_parent

关于为什么会发生这种情况的任何想法?我可以通过 xml 设置另一个参数还是可以以编程方式调用来解决此问题的方法?

谢谢!

ICS 中的正确行为:

Correct Behavior in ICS

GingerBread 中的错误行为: Incorrect Behavior in GingerBread

XML 布局:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
        <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/speech_bubble_framelayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@color/color_transparent"
            android:layout_toRightOf="@id/message_user_imageView">

        <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
                   android:id="@+id/message_background_imageview"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:layout_margin="5dp"
                   android:scaleType="fitXY"
                   android:layout_centerInParent="true"
                   android:textColor="@color/color_brown_mud"
                   android:background="@drawable/chat_bubble_flipped"
                />
        <TextView
                android:id="@+id/message_textView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:maxEms="10"
                android:background="@color/color_transparent"
                android:textColor="@color/color_brown_uiDesign_pallete"
                android:textSize="15sp"
                android:textStyle="bold"
                android:gravity="center_horizontal"
                android:text="Message"
                android:padding="5dp"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                />
    </FrameLayout>
</merge>

【问题讨论】:

    标签: android android-layout android-fragments android-imageview android-listfragment


    【解决方案1】:

    从外观上看,这是由您的尺寸的循环依赖性引起的:内部文本和图像大小取决于 FrameLayout (match_parent),而 FrameLayout 的大小取决于其中包含的内容 (@ 987654322@)。如果正是出于这种原因涉及多个孩子,则不鼓励使用 FrameLayout。为了安全起见,我建议将FrameLayout 更改为RelativeLayout,并在内部图像视图上设置以下属性:

    android:layout_alignTop="@+id/message_textview"
    android:layout_alignBottom="@+id/message_textview"
    

    这将确保您的图像始终与您的文本大小相匹配。

    【讨论】:

    • 解决了这个问题。我使用了带有一些填充的 alignBottom 和 alignRight。谢谢!
    猜你喜欢
    • 2012-08-18
    • 2012-07-31
    • 1970-01-01
    • 2018-06-23
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    相关资源
    最近更新 更多