8

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

我通过将textView设置为wrap_content,将语音气泡imageView设置为match_parent并将frame_layout设置为wrap_content来实现这一点。这样文本后面的 speech_bubble 图像会 根据气泡中的文本量进行缩放。 

我将 textView 上的宽度设置为与父级匹配,将高度设置为 wrap_content,由于某种原因,它在Ice Cream Sandwich (Android 4.1)中完美地工作。然而,在Gingerbread中,内部 imageView 语音气泡不会缩放到 match_parent,因此文本可以整齐地放入气泡内。在 Gingerbread 中,内部的 imageView始终保持相同的大小,而不管文本和文本溢出气泡外。即使frameLayout扩展为wrap_contentimageView也不会像预期的那样match_parent。 

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

谢谢!

ICS 中的正确行为:

ICS 中的正确行为

GingerBread 中的 错误行为: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>
4

1 回答 1

27

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

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

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

于 2012-09-15T05:34:08.823 回答