我試圖實現一個非常簡單的佈局,但當高度變得太小時(橫向模式),我無法讓它工作正常, 。RelativeLayout:除非視圖太小,否則在屏幕中居中放置東西
我想要的是在屏幕中水平和垂直居中,並將B置於A之上的空間(與頂部對齊或高於A,我不介意)。這在肖像中效果很好(參見圖1)。 當高度太小時,我想讓A不重疊B(它在圖2中)。我希望它保持在低於(尊重B的底部邊距)和ScrollView
以完成其工作(圖3)。
總之,我想A到在屏幕居中除非B(和它的頂部/底部邊緣)不能符合上述A.
我試圖通過把一個來實現這一和B在RelativeLayout
其中A是中心和B是alignedParentTop
與頂部邊距(我也嘗試B對齊以上A與底部邊距)。所有這些都包裹在ScrollView
中。 在這兩種情況下,重疊都是不可避免的。 (請參閱下面的XML)
SO上的RelativeLayout
重疊問題的大多數解決方案指的是將某些東西置於LinearLayout
(可能有一些權重)。 如果我想讓A在屏幕中居中(而不是B和底部之間),這些解決方案並不適用。
你有什麼想法我可以得到這個工作?謝謝你的幫助。
這是到目前爲止我的佈局代碼:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- B -->
<TextView
android:id="@+id/recommend_text"
style="@style/TextAppearance.SFR.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:layout_marginBottom="40dp"
android:gravity="center_horizontal"
android:text="@string/recommend_screen_text" />
<!-- A -->
<LinearLayout
android:id="@+id/recommend_buttons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<!-- content -->
</LinearLayout>
</RelativeLayout>
</ScrollView>
您可以查看片段。然而,Android的最佳做法是每個尺寸需要支持一種佈局...... – Johan
不要使用'RelativeLayout'來代替使用垂直方向的'LinearLayout' –
@Kartheeks我在我的問題中提到,這無法實現,因爲我希望我的A視圖在屏幕中垂直居中。因此,對於A,權重= 1的'LinearLayout'不是解決方案。如果你看到'LinearLayout'可以做到這一點,我會很高興你能分享(出於好奇,即使我現在已經有了解決方案)。 – Joffrey