2013-03-25 53 views
1

我試圖編寫我的第一個android應用程序。我讀了很多,但是我的佈局有問題。 這兩個xml有什麼問題? 爲了更好地理解,我做了一個畫面:pic問題水平/垂直佈局Android應用程序

我的XML爲main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:orientation="vertical" > 



<ImageView 

    android:id="@+id/imageView1" 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:layout_weight="2.5" 

    android:contentDescription="@string/upload" 

    android:scaleType="fitCenter" 

    android:visibility="visible" /> 



<LinearLayout 

    android:layout_width="match_parent" 

    android:layout_height="0dp" 

    android:layout_weight="4" 

    android:gravity="center" 

    android:orientation="vertical" > 



    <Button 

     android:id="@+id/btnShoot" 

     android:layout_width="match_parent" 

     android:layout_height="wrap_content" 

     android:layout_gravity="center" 

     android:layout_marginLeft="50dp" 

     android:layout_marginRight="50dp" 

     android:text="@string/shoot_again" /> 

</LinearLayout> 
</LinearLayout> 

在文件夾佈局土地main.xml中,如果我切換到橫向模式的應用程序崩潰:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:orientation="horizontal" > 



<Button 

    android:id="@+id/btnShoot" 

    android:layout_width="match_parent" 

    android:layout_height="wrap_content" 

    android:layout_gravity="center" 

    android:layout_marginLeft="50dp" 

    android:layout_marginRight="50dp" 

    android:text="@string/shoot" /> 



<ImageView 

    android:id="@+id/imageView1" 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:contentDescription="@string/upload" 

    android:scaleType="fitCenter" 

    android:visibility="visible" /> 

</LinearLayout> 
+0

,怎麼了? – 2013-03-25 10:55:56

+0

顯示您的錯誤日誌。 – 2013-03-25 10:56:40

回答

0

好了,只是看你的XML,有幾個問題:

肖像XML:

< ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="2.5" 

在這裏,你已經同時設置了重量和高度 - 在LinearLayout你不能兼得。要麼你自己設置高度,要麼給它一個高度0dp和一個重量,然後讓LinearLayout指定高度。至於它爲什麼旋轉,圖像本身可能會在您的資源中旋轉。

水平XML:

  • 你的根LinearLayout仍然需要有一個vertical方向
  • 你的ImageView需要您的按鈕

發生了什麼事是,)它的水平以上聲明, b)您的按鈕首先被放置,c)您的按鈕有android:layout_width="match_parent",這意味着ImageView無處可放,因爲您的Button已經佔用了所有在屏幕上的空間(你可以看到這是爲什麼呢?)

進行這些更改,然後更新您的問題在適當時,我認爲,當你說「崩潰」你的問題就已經改變了