我正在开发一个基本的 Android 应用程序,其主要 Activity 的布局中有 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/