2012-02-29 77 views
1

我一直在開發一個應用程序在Android上開始。我所遇到的問題,我的佈局將無法正常顯示,當它轉到其他像這樣的不同方向:佈局不能正確地重新調整方向更改

http://imgur.com/a/aPe2p

這裏是我的垂直佈局代碼:

<?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="match_parent" 
    android:orientation="vertical" 
    android:padding="3dip" > 

    <EditText 
     android:id="@+id/passText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="5" 
     android:hint="Passcode" 
     android:inputType="textPassword" /> 

    <EditText 
     android:id="@+id/messageText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="top|left" 
     android:hint="Message" > 

     <requestFocus /> 
    </EditText> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="5" 
     android:orientation="horizontal" 
     android:weightSum="100" > 

     <Button 
      android:id="@+id/encodeButton" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:text="Encode" /> 

     <Button 
      android:id="@+id/decodeButton" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:text="Decode" /> 
    </LinearLayout> 

</LinearLayout> 

這裏是我的景觀佈局代碼:

<?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="match_parent" 
    android:orientation="vertical" 
    android:padding="8dip" > 

    <EditText 
     android:id="@+id/passText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:hint="Passcode" 
     android:inputType="textPassword" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/messageText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:gravity="top|left" 
     android:hint="Message" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:orientation="horizontal" 
     android:weightSum="100" > 

     <Button 
      android:id="@+id/encodeButton" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:text="Encode" /> 

     <Button 
      android:id="@+id/decodeButton" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="50" 
      android:text="Decode" /> 
    </LinearLayout> 

</LinearLayout> 
+0

我寧願建議你使用'RelativeLayout'實現這個 – waqaslam 2012-02-29 21:46:08

+0

能否請您顯示出與使用該佈局的活動清單片段? – muffinmad 2012-02-29 22:54:36

+0

我通過使用相對佈局得到了這個。謝謝 – globalninja 2012-02-29 23:37:48

回答

1

嘗試設置上的項目您android:layout_height擴大/縮小到wrap_content。如果你讀到這裏:

http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#attr_android:layout_height

它談到了三個可能的值和他們做什麼。

fill_parent:該視圖應該與其父(減去填充)一樣大。該常數從API Level 8開始被棄用,並由match_parent取代。

match_parent:該視圖應該與其父(減去填充)一樣大。在API級別8中引入。

wrap_content:視圖應該只有足夠大以包含其內容(加上填充)。

你應該在這些項目中使用wrap_content。下面我添加了高度的我會改變你的垂直佈局的例子:

<EditText 
    android:id="@+id/passText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="5" 
    android:hint="Passcode" 
    android:inputType="textPassword" /> 

<EditText 
    android:id="@+id/messageText" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="top|left" 
    android:hint="Message" > 

    <requestFocus /> 
</EditText> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="5" 
    android:orientation="horizontal" 
    android:weightSum="100" > 

    <Button 
     android:id="@+id/encodeButton" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="50" 
     android:text="Encode" /> 

    <Button 
     android:id="@+id/decodeButton" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="50" 
     android:text="Decode" /> 
</LinearLayout>