[英]How to place bottomAppBar in layout
我目前正在开发一个使用底部应用栏作为其主要导航的项目。 但即使我从示例源代码中复制粘贴,我也总是收到错误消息
这是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"></com.google.android.material.bottomappbar.BottomAppBar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
这是我的 gradle 依赖项
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
这是我得到的错误
Binary XML file line #14: Binary XML file line #14: Error inflating class com.google.android.material.bottomappbar.BottomAppBar
根据此网站:将您的 targetSdkVersion 设置为针对 Android P 的最新 API 版本,即 28,并确保您的应用继承Theme.MaterialComponents
主题,以使 BottomAppBar 使用最新样式。 或者,您可以在布局 xml 文件中的小部件声明中声明BottomAppBar
的样式,如下所示: style="@style/Widget.MaterialComponents.BottomAppBar"
我最近解决了这个问题。 你需要添加依赖
implementation 'com.google.android.material:material:1.0.0
然后将<com.google.android.material.bottomnavigation.BottomNavigationView
添加到您的 XML 布局文件中。
它对我有用
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="wrap_content" android:id="@+id/vBottomBar"
app:layout_constraintHorizontal_bias="0.0"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/vBottomBar" android:id="@+id/vContainer">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我昨晚找到了解决我的问题的方法。 我的问题是我使用
implementation 'com.google.android.material:material:1.1.0-alpha05'
并且那个版本仍然是 alpha 并且不稳定。 所以我把它降级为
implementation 'com.google.android.material:material:1.0.0'
一切都像黄油一样顺畅
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.