【问题标题】:Problems creating bottom Navbar Android Studio创建底部导航栏 Android Studio 的问题
【发布时间】:2019-03-27 22:02:40
【问题描述】:

几天来,我一直在尝试为我的应用创建一个底部导航栏,但由于某种原因,它只是没有显示在设计视图中。使用 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>

现在我想要发生的是,我可以在活动的底部正确地看到设计,但它只是没有显示出来。

【问题讨论】:

    标签: java android navigationbar androidx


    【解决方案1】:

    我创造了一些非常相似的东西。在设计中,对我来说它不会出现在底部。相反,它显示在顶部(操作栏)。但是当我编译和构建项目并在模拟器或设备上运行它时,它显示为底部导航栏。

    在我的 MainActivity.java 中,我正在用这段代码加载它:

    //show bottom navigation bar
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    BottomNavigationViewHelper.disableShiftMode(navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    

    【讨论】:

      【解决方案2】:

      尝试不指定固定高度,而是在 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-20
        • 1970-01-01
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 2020-12-10
        • 2020-07-08
        • 2022-07-11
        相关资源
        最近更新 更多