我想在我的Android應用實現以下佈局:Android:何時使用layout_margin,何時使用weightSum?
正如你可以看到我想要一箇中央登錄框取寬度的約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」屬性。
您認爲哪一個是最好的方法?提前致謝。
這幾乎是方法n。 1我在我的問題中提到不是嗎? –
我會說一個重要的筆記。您爲父級佈局建議了相對或線性佈局。我選擇親戚,因爲這允許我使用layout_centerInParent參數。在這個過程中,將保證金設置爲您認爲看起來不錯的程度是微不足道的。 – Gafanha
即使是線性的,這也可以工作。你只需要android:gravity =「center」來代替。 –