1

我使用的FrameLayout和內,我有2個TextView的,如果我使用邊距未Android平臺2.2

android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 

我的佈局沒有顯示在Android 2.2的正確,而在Android上同樣做工精細4.0。

這裏是完整的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

      <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:gravity="left|center" 
      android:text= "Testing String" 
      android:textSize="15sp" 
      android:textColor="@color/LIGHTGRAY" 

      android:layout_gravity="left|top"/> 

     <TextView 

      android:id="@+id/ID_due_date" 
      android:textStyle="bold" 
      android:textSize="15sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="10dp" 
      android:text= "Testing String" 
      android:layout_marginTop="10dp" 
      android:layout_gravity="right" 
      android:gravity="left|center"/> 
     </FrameLayout> 
</ScrollView> 

在Android 2.2系統,它看起來像這樣

enter image description here

在搭載Android 4.0 ICS,它看起來像這樣

enter image description here

回答

3

有Android 2.2中(一個bug Froyo)和2.3(薑餅)FrameLayout內的利潤不適用於沒有的孩子屬性集。你可以在這裏看到:https://code.google.com/p/android/issues/detail?id=28057
這個bug在Android 3.0(Honeycomb)中得到修復。

您的問題與此錯誤有關。你確實設置了一個layout_gravity屬性,因此正確應用邊距 - 視圖從頂部和側面偏移10dp。不幸的是FrameLayout不知道如何衡量這樣的孩子,所以它的邊界計算沒有這些利潤率 - 這就是爲什麼孩子被切割在底部(10dp)。

FrameLayout與高度match_parent不會影響此問題,但在您的情況下,您有wrap_content行爲設置。 (請注意即使您將高度設置爲fill_parent它仍然會以wrap_content的形式工作,因爲它在ScrollView之內)。

請嘗試使用填充代替 - 在大多數情況下(背景或可點擊區域不重要)邊距可以用填充替換。

+0

是,填充作品。但如果我轉移到ICS或其他更高版本,它不會給我帶來任何問題嗎? – Naruto

+0

不,在這種情況下填充物不會有問題。你可以在ICS(或更高版本)上測試它,如果你想:) –

+0

謝謝你的幫助:) – Naruto

1

您可以使用填充:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left|top" 
      android:gravity="left|center" 
      android:paddingLeft="10dp" 
      android:paddingTop="10dp" 
      android:text="Testing String" 
      android:textColor="@color/LIGHTGRAY" 
      android:textSize="15sp" /> 

     <TextView 
      android:id="@+id/ID_due_date" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:gravity="left|center" 
      android:paddingRight="10dp" 
      android:paddingTop="10dp" 
      android:text="Testing String" 
      android:textSize="15sp" 
      android:textStyle="bold" /> 
    </FrameLayout> 

</ScrollView> 
+0

謝謝+1,爲您的答案。謝謝你 – Naruto