2012-10-12 29 views
1

我想在我的Android應用實現以下佈局:Android:何時使用layout_margin,何時使用weightSum?

enter image description here

正如你可以看到我想要一箇中央登錄框取寬度的約70%。我相信基本上有兩種方法可以實現這一點:

1.爲登錄框創建線性或相對佈局,並在左側和右側添加layoutMargin。

2.創建weightSum = 「1」 的包裝的LinearLayout,然後放置RelativeLayout的裏面與layout_width = 「0dp」 和layout_weight = 「0.7」。

使用第一種方法的XML是這樣的:

<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:orientation="vertical"> 

    <ImageView 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:src="@drawable/logo" 
     android:contentDescription="@string/logo" 
     /> 

    <LinearLayout 
     android:layout_width="match_parent"   
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="#999999" 
     android:gravity="center" 
     android:layout_margin="30dp" 
     > 
     <EditText 
      android:layout_width="fill_parent"   
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:hint="@string/email" /> 
     <EditText 
      android:layout_width="fill_parent"    
      android:layout_height="wrap_content" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:hint="@string/password" /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/login"/> 


    </LinearLayout>  


</LinearLayout> 

第二種方法將只對當前的一種包裝的LinearLayout,與weightSum =「1」,內部佈局會得到一個layout_weight =「0.7」屬性。

您認爲哪一個是最好的方法?提前致謝。

回答

2

一個相當簡單的方法是在相對佈局中使用線性佈局。然後使用layout_centerInParent並將其設置爲「true」。一旦完成,您可以將您的保證金設置爲您想要的值(我使用了30 dp)。這裏有一個例子:

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

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_margin="30dp" 
    android:orientation="vertical" 
    android:weightSum="50" > 

    <EditText 
    android:id="@+id/editText1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:inputType="textEmailAddress" > 

    <requestFocus /> 
</EditText> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/editText1" 
    android:layout_centerHorizontal="true" 
    android:inputType="numberPassword" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/editText2" 
    android:layout_centerHorizontal="true" 
    android:text="Button" /> 
</LinearLayout> 

</RelativeLayout> 

當然,如果你想在日誌中框的寬度是屏幕的寬度正好70%,你可以做到這一點編程。

+0

這幾乎是方法n。 1我在我的問題中提到不是嗎? –

+0

我會說一個重要的筆記。您爲父級佈局建議了相對或線性佈局。我選擇親戚,因爲這允許我使用layout_centerInParent參數。在這個過程中,將保證金設置爲您認爲看起來不錯的程度是微不足道的。 – Gafanha

+0

即使是線性的,這也可以工作。你只需要android:gravity =「center」來代替。 –

0

還有一個3d選擇,在LinearLayout/RelativeLayout中創建登錄框,然後水平對齊它。

+0

但是,如果我這樣做,而不使用邊距登錄框將採取整個寬度,這不是我想要的。如果我在登錄框中使用width =「wrap_content」,則需要爲EditTexts設置固定寬度。 –

+0

從我的角度來看,在這種情況下不需要邊距,只需讓登錄框'layout_width =「wrap_content」'(和layout_height也),它將佔用盡可能多的空間。 –

+0

但是,你將如何完成在登錄框內具有相同寬度的EditTexts? –

相關問題