我已经尝试了几天为我的应用程序创建一个底部导航栏但由于某种原因它只是没有出现在设计视图中。并没有真正帮助这个事实,即没有很多使用AndroidX库的教程
(对不起,如果我没有使用正确的词语来描述事物)
试图改变设计的大小,调整依赖关系并重试多次。
/// build.gradle file
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
/// Build.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.runwithmeapp"
minSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'com.google.android.material:material:1.0.0-rc01'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation 'com.github.parse-community.Parse-SDK-Android:fcm:1.19.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
implementation 'com.android.support:design:28.0.0'
}
/// XML for activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
tools:context=".BottomNav">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="78dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dp"
app:menu="@menu/bottom_navigation" />
</RelativeLayout>
/// XML for my menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home"/>
<item
android:id="@+id/nav_Map"
android:icon="@drawable/ic_map_black_24dp"
android:title="Map"/>
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_person_black_24dp"
android:title="Profile"/>
</menu>
现在我想要发生的是,我可以在活动的底部正确看到设计,但它只是没有出现。
我创造了非常相似的东西。在设计中,对我而言,它并没有显示在底部。相反,它出现在顶部(Action Bar)。但是当我编译和构建项目并在模拟器或设备上运行它时,它会显示为底部导航栏。
在我的MainActivity.java中,我用这段代码加载它:
//show bottom navigation bar
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
尝试不指定固定高度,而是在BottomNavigationView中使用wrap_content。另外,不推荐使用Relativelayout,而是使用Constraintlayout。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@color/item_background"
app:itemIconTint="@color/item_tint"
app:itemTextColor="@color/item_text_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu_bottom_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>