這是因爲你只適用marginLeft
到第一TextView
,不是他們兩個。如果您想將marginLeft
同時適用於TextViews
,你可以用LinearLayout
圍繞着他們兩個,然後給LinearLayout
的300dp一個marginLeft
或者你可以給雙方TextViews
一個marginLeft
的300dp
下面是代碼的包裝都在TextViews
一個LinearLayout
:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
//Other stuff here
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:text="use margin left view 1"
android:id="@+id/text"
android:textSize="40sp"
android:background="@color/colorPrimaryDark"/>
<TextView
android:layout_width="290dp"
android:layout_height="200dp"
android:text="not affected by margin view 2 "
android:textSize="40sp"
android:background="@color/colorPrimaryDark"/>
</LinearLayout>
//Other stuff here
</RelativeLayout>
你也可以只使用一個RelativeLayout
的屬性來獲取諸如alignLeft
相同的期望結果。
下面是代碼:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="300dp"
android:text="use margin left view 1"
android:textSize="40sp"
android:background="@color/colorPrimaryDark"/>
<TextView
android:layout_width="290dp"
android:layout_height="200dp"
android:layout_alignLeft="@id/text"
android:text="not affected by margin view 2 "
android:textSize="40sp"
android:background="@color/colorPrimaryDark"/>
</RelativeLayout>
其他RelativeLayout
屬性,嘗試可以在這裏找到:https://developer.android.com/reference/android/widget/RelativeLayout.html
,這裏是一些正在使用的例子: https://developer.android.com/guide/topics/ui/layout/relative.html