8

我正在嘗試爲聊天室創建一個語音氣泡。每個講話泡泡都有附帶的xml佈局。Android ImageView在下面的FrameLayout裏面不match_parent GingerBread

我通過設置的TextView實現這一WRAP_CONTENT,語音泡沫的ImageViewmatch_parentframe_layoutWRAP_CONTENT。以便文字後面的speech_bubble圖像根據泡泡中文字的大小進行縮放。

我設置寬度上TextView的匹配父和WRAP_CONTENT高度,出於某種原因,它作爲Ice Cream Sandwich的(安卓4.1)需要完美的作品。 但薑餅內部imageView語音氣泡不縮放到match_parent,使文本可以整齊地放在泡泡內。 在薑餅的內部圖像查看保持相同的大小始終,無論文字和文字溢出氣泡之外。儘管 的的FrameLayout擴展到WRAP_CONTENT,則ImageView的match_parent,因爲它應該。

爲什麼會發生這種情況的任何想法?是否有另一個參數,我可以設置通過xml或我可以調用編程方式來解決這個問題的方法?

謝謝!在ICS

正確行爲:

Correct Behavior in ICS

薑餅不正確的行爲: 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> 

回答

24

從外觀上來看,這是一個引起循環依賴於您的尺寸:內部文本和圖像尺寸取決於FrameLayout( match_parent),而FrameLayout的大小取決於其中包含的內容(wrap_content)。出於這種原因,在涉及多個孩子的情況下,不鼓勵使用FrameLayout。爲了安全起見,我建議改變FrameLayoutRelativeLayout,並設置在內部圖像視圖以下屬性:

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

這將確保圖像始終匹配到你的文字的大小。

+0

解決了這個問題。我用alignBottom和alignRight與一些填充。謝謝! – Bala

相關問題