我正在编写一个 Android Activity ,它的上侧应该有一种颜色的背景,而下侧应该有另一种颜色的背景。
使用的相对布局已填充。
我通常做的是使用非填充相对布局并将其分为上下相对布局,这些 u/l 分为其他填充相对布局。
这让我确信所有 Activity 区域都有它应该有的背景颜色。 填充区域确保小部件位于 Activity 中心周围。
但是现在我有一个已经编程的 Activity ,并且上面的小部件和底部的小部件彼此相关,所以我无法轻松地划分相对布局。
有什么建议吗?
最佳答案
查看适合您的 Activity 的布局。只有一个具有两个背景的 RelativeLayout
。一个设置为布局本身,另一个设置为位于顶部 View 下方的空 View
。唯一的缺点是您必须从 RelativeLayout
中删除 padding
并将其替换为为您的 View 设置的 margin
。我想这没什么大不了的。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#59C2F2">
<!-- This is the last TextView of top part, below it the bg will be different -->
<TextView
android:id="@+id/top_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="This represents top elements"/>
<!-- This View must be placed before any elements from the bottom part -->
<View
android:id="@+id/bottom_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/top_text"
android:layout_alignParentBottom="true"
android:background="#004892" />
<!-- Now you can place your bottom elements and align them with the to ones -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/bottom_background"
android:layout_alignLeft="@+id/top_text"
android:layout_marginTop="16dp"
android:textColor="#fff"
android:text="This is the bottom part"/>
</RelativeLayout>
关于android - Android 的两种颜色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29890557/