java - Android 布局分为 3 个部分

标签 java android

我正在开发一个基本的 Android 应用程序,其主要 Activity 的布局中有 3 个“部分” ->

  1. 顶部的广告横幅
  2. 主要内容部分
  3. BottomNavigationView 位于底部

我的布局有效,但顶部的广告横幅与一些主要部分重叠(注意:广告横幅是动态创建的,并使用 match_parent 插入到 ad_layout 中> 宽度和 wrap_content 高度)。我希望广告横幅占据自己的空间,然后让主要部分占据自己的空间,然后让 BottomNavigationView 占据自己的空间。

我现在拥有的 XML 看起来一团糟。有没有更好的方法来拥有 3 个不同的部分,每个部分都占据自己的空间?谢谢

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/outer_layout">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_layout"
        android:background="@android:color/white">
    <!-- Main Content -->
<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/toolbar_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/primary_dark"
    app:itemIconTint="@android:color/white"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/toolbar_bottom_menu"
    app:labelVisibilityMode="unlabeled"/>
        <FrameLayout
            android:id="@+id/ad_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

最佳答案

您可以使用ConstraintLayout:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/outer_layout">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent>

        <FrameLayout
            android:id="@+id/ad_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/main_layout"
            android:background="@android:color/white"
            app:layout_constraintTop_toBottomOf="@+id/ad_layout"
            app:layout_constraintBottom_toTopOf="@+id/toolbar_bottom"/>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/toolbar_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/primary_dark"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:menu="@menu/toolbar_bottom_menu"
            app:labelVisibilityMode="unlabeled"
            app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.constraintlayout.widget.ConstraintLayout/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

关于java - Android 布局分为 3 个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445644/

相关文章:

android - 将 Assets 从 watch 转移到手持设备

android - 在工具栏上放置自动完成搜索

java - 蓝牙启动发现未给出结果

java - 创建具有 2D 通用数组字段的通用 Java 类

java - 急流。如何在不渲染的情况下从 lambda 返回响应?

java - 使用 colspan 和 rowspan 从二维数组生成 html 表

android - 如何远程调试 Android 且泄露 PII 的风险较低?

c# - 与 C# 不同的 Java 数字签名

java - Java 打印多维数组

android - Android 中的 Theme.AppCompat 和 Theme.Material 有什么区别?