【问题标题】:Bottom navigation view does not appear in emulator, only in layout preview底部导航视图不会出现在模拟器中,仅出现在布局预览中
【发布时间】:2020-10-30 21:41:49
【问题描述】:

导航底部视图未显示我没有看到代码有任何问题,尽管它显示在预览中。 启动主要活动后,它不会显示。 请注意,我在检查用户是否登录的启动活动中使用底部导航视图启动主要活动。

这是启动活动布局文件

package io.keepcoding.androidfinalproject.ui

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import io.keepcoding.androidfinalproject.R
import io.keepcoding.androidfinalproject.ui.auth.AuthActivity
import io.keepcoding.androidfinalproject.ui.main.MainActivity

class LaunchingActivity : AppCompatActivity() {

    private var userLoggedIn: Boolean = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_launch)
        if(userLoggedIn){
            startAppActivity()
        } else {
            startAuthActivity()
        }
    }

    private fun startAuthActivity() {
        val intent = Intent(this, AuthActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun startAppActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }
}

这是未显示的启动 Activity 布局文件,因为如果用户登录,应用程序一旦启动,主 Activity 就会启动。

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.LaunchingActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的主要活动布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.MainActivity"
    >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigationView"
        app:labelVisibilityMode="labeled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/app_bar"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/bottom_nav_bar_unselected"
        app:menu="@menu/bottom_nav_menu"/>

</RelativeLayout>

这是我的主要活动代码

package io.keepcoding.androidfinalproject.ui.main

import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.replace
import com.google.android.material.bottomnavigation.BottomNavigationView
import io.keepcoding.androidfinalproject.R
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    private lateinit var toolbar: ActionBar
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.activity_main)
        toolbar = supportActionBar!!
        val bottomNavigation: BottomNavigationView = navigationView
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {
        when(it.itemId) {
            R.id.navigation_products -> {
                toolbar.title = "Products"
                val productFragment = ProductsFragment.newInstance()
                openFragment(productFragment)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_map -> {
                toolbar.title = "Map"
                val mapFragment = ProductsFragment.newInstance()
                openFragment(mapFragment)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_profile -> {
                toolbar.title = "Profile"
                val profileFragment = ProductsFragment.newInstance()
                openFragment(profileFragment)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                toolbar.title = "Notifications"
                val notificationsFragment = ProductsFragment.newInstance()
                openFragment(notificationsFragment)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    private fun openFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.container, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }
}

这里是菜单布局文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_products"
        android:icon="@drawable/ic_products"
        android:enabled="true"
        android:title="@string/title_products" />

    <item
        android:id="@+id/navigation_map"
        android:icon="@drawable/ic_map"
        android:enabled="true"
        android:title="@string/title_map" />

    <item
        android:id="@+id/navigation_sell"
        android:icon="@drawable/ic_add"
        android:enabled="true"
        android:title="@string/title_sell"
        />

    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/ic_profile"
        android:enabled="true"
        android:title="@string/title_profile"
        />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications"
        android:enabled="true"
        android:title="@string/title_notifications" />

</menu>

您可以在此处找到该项目的链接

https://github.com/kcFinalProject/AndroidFinalProject/tree/master

【问题讨论】:

    标签: android android-layout kotlin bottomnavigationview


    【解决方案1】:

    您的 activity_main.xml 是具有导航栏的那个吗?

    如果是这种情况,也许您的问题是 Frame Layour 高度为 match_parent

    要查看是否是问题所在,请将框架布局可见性设置为不可见

    将属性android:visibility="invisible"添加到FrameLayout,然后查看导航栏是否可见。

    如果导航栏现在可见,那么您必须在导航栏顶部设置 FrameLayout 的底部约束。在 FrameLayout 中添加另一个属性android:layout_above="@id/navigationView"

    【讨论】:

      【解决方案2】:

      已解决,我覆盖了错误的 onCreate 方法,因此从 Launching Activity 中启动的主要 Activity 从未真正运行过。

      onCreate not called

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-19
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-16
        相关资源
        最近更新 更多