2013-07-25 82 views
1

我的目標是顯示幾個按鈕,然後顯示一個PNG圖像。問題是圖像根本不顯示。如果我拿出所有按鈕,圖像將顯示。這個XML文件究竟有什麼問題?ImageView不顯示在LinearLayout中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:adjustViewBounds="true" 
    tools:context=".MainActivity" > 



    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/option1" 
     android:onClick="sendMessage" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/OR" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/option2" 
     android:onClick="sendMessage" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/Pic" 
     android:src="@drawable/soup" /> 

</LinearLayout> 
+0

默認情況下,Linearlayout的方向是水平的。使其垂直。 – Geros

回答

2

LinearLayout將佈置每個孩子直到空間不足。如果前三個孩子的屏幕寬度至少與屏幕寬度一樣多,我們不會在屏幕上顯示ImageView(我稱寬度,因爲LinearLayouts默認爲水平 - 如果您希望它是垂直的,則必須包含android:orientation="vertical"屬性)。

您或者需要指定佈局權重來爲每個chid指定可用空間的某個部分,或者您需要選擇更適合您需要的另一個佈局。

0

將「android:orientation」的線性佈局設置爲水平或垂直。這可能會解決它。

0

沒有什麼不對。由於您對各個視圖使用「wrap_content」,因此您可能會跨越佈局邊界。在這種情況下,您可以設置佈局權重以適應所有情況。