2012-01-31 74 views
1

我遇到了爲android按鈕屬性分配的負值,如下所示。爲Android按鈕屬性分配負值

android:layout_marginTop="-37px" 

有沒有人有任何想法,究竟是什麼意思......?在此先感謝...

回答

4

負邊距可用於使佈局管理器在定位時看起來更小。

因此,例如,想象一個視圖,其高度爲h,邊緣頂部爲-m。當這個觀點被定位後,經理會認爲該視圖的頂部是-m而不是0。在線性佈局的情況下(假設垂直佈局),這將導致視圖被渲染在先前的視圖之上。

您可以在下面的示例中看到這一點,當您減小textView2的頂部邊距時,它會覆蓋在textView1上。

<?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" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="-15dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

無論這是否是指定的行爲,我不是100%確定。在這post Romain Guy提到你可以使用負邊距,但是在Google Groups上的這個post他提到負邊距行爲沒有指定。