android - Android 的两种颜色背景

标签 android android-layout android-activity background background-color

我正在编写一个 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/

相关文章:

android - 如何在 Android 模拟器上运行 Xamarin UI 测试

android - 是否可以在Android中删除子填充

android - 正确的onDestroy()/如何避免内存泄漏

java - 为什么我的 Activity 在添加 fragment 后会被破坏(#2)?

Android,ADB,将 STDOUT 重定向到套接字

android - 将 Android 中的 EditText 设置为 null 后恢复 KeyListener 会丢失输入类型选项

Android - 如何在布局中存储额外的隐藏信息(按标签)

android - 更改可聚焦 ViewGroup 中元素的阅读顺序

android - 如何让多个 Intent 过滤器在同一个任务中启动一个 Activity

android - Android 音频编解码器