框架布局占据屏幕并隐藏底部导航和工具栏

问题描述 投票:1回答:2

我正在尝试将当前使用android导航组件的设置切换为使用经典片段管理器,因为导航组件给了我很多限制,我无法解决。

我面临着一个问题,因为当我开始一个事务以在我的框架布局中显示一个片段时,突然他的底部导航和工具栏不再显示了。我尝试过不同的东西,但我无法找到解决方案的原因。

这是我的活动布局:

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


    <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:theme="@style/MyActionBar"
            android:minHeight="?attr/actionBarSize"
            android:id="@+id/my_toolbar"/>

    <include
            layout="@layout/content_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/feed_bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="?android:attr/windowBackground"

            app:itemBackground="@color/white"
            app:itemTextColor="@color/bottom_nav_color"
            app:itemIconTint="@color/bottom_nav_color"
            app:menu="@menu/navigation"
            app:labelVisibilityMode="unlabeled"/>

    </LinearLayout>

这是我包含的content_main的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="FeedActivity"
             app:layout_behavior=""
             tools:showIn="@layout/activity_feed"
             android:padding="1dp"
             android:id="@+id/feed_frame_container">

</FrameLayout>

这是我的活动课:

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import co.getdere.Fragments.FeedFragment
import co.getdere.Fragments.ImageFullSizeFragment
import co.getdere.Fragments.ProfileRandomUserFragment
import co.getdere.Models.Images
import co.getdere.Models.Users
import co.getdere.RegisterLogin.RegisterActivity
import co.getdere.ViewModels.SharedViewModelCurrentUser
import co.getdere.ViewModels.SharedViewModelImage
import co.getdere.ViewModels.SharedViewModelRandomUser
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener


class FeedActivity : AppCompatActivity() {

    lateinit var mToolbar: Toolbar
    lateinit var mBottomNav: BottomNavigationView

    var imageFullSizeFragment = ImageFullSizeFragment()
    var feedFragment = FeedFragment()
    var randomProfileNavHostFragment = ProfileRandomUserFragment()

    val fm = supportFragmentManager
    var active = feedFragment


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_feed)

        FirebaseApp.initializeApp(this)
        checkIfLoggedIn()


        mToolbar = findViewById(R.id.my_toolbar)
        setSupportActionBar(mToolbar)

        mBottomNav = findViewById(co.getdere.R.id.feed_bottom_nav)

        mBottomNav.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {

                co.getdere.R.id.destination_board -> {

                    val intent = Intent(this, BoardActivity::class.java)
                    startActivity(intent)
                }
                R.id.destination_profile_logged_in_user -> {

                    val intent = Intent(this, ProfileActivity::class.java)
                    startActivity(intent)
                }

                R.id.destination_feed -> {

                    val intent = Intent(this, ProfileActivity::class.java)
                    startActivity(intent)
                }
            }
            false
        }


        fm.beginTransaction().add(R.id.feed_frame_container, imageFullSizeFragment, "imageFullSizeFragment").hide(imageFullSizeFragment).commit()
        fm.beginTransaction().add(R.id.feed_frame_container, randomProfileNavHostFragment, "randomProfileNavHostFragment").hide(randomProfileNavHostFragment).commit()
        fm.beginTransaction().add(R.id.feed_frame_container, feedFragment, "feedFragment").commit()

    }


    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(co.getdere.R.menu.feed_navigation, menu)
        return super.onCreateOptionsMenu(menu)
    }

    private fun checkIfLoggedIn() {

        val uid = FirebaseAuth.getInstance().uid
        if (uid == null) {
            val intent = Intent(this, RegisterActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
        } else {
            fetchCurrentUser()
        }

    }

    private fun fetchCurrentUser() {

        val uid = FirebaseAuth.getInstance().uid
        val ref = FirebaseDatabase.getInstance().getReference("/users/$uid/profile")
        ref.addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onCancelled(p0: DatabaseError) {
            }

            override fun onDataChange(p0: DataSnapshot) {


                sharedViewModelCurrentUser.currentUserObject = p0.getValue(Users::class.java)!!
                Log.d("checkLocation", "fetchCurrentUser")

            }

        })
    }


    companion object {
        fun newInstance(): FeedActivity = FeedActivity()
    }
}

如果我现在运行应用程序,框架布局中的片段将占据整个屏幕,但如果我删除这些行:

        fm.beginTransaction().add(R.id.feed_frame_container, imageFullSizeFragment, "imageFullSizeFragment").hide(imageFullSizeFragment).commit()
        fm.beginTransaction().add(R.id.feed_frame_container, randomProfileNavHostFragment, "randomProfileNavHostFragment").hide(randomProfileNavHostFragment).commit()
        fm.beginTransaction().add(R.id.feed_frame_container, feedFragment, "feedFragment").commit()

我可以再次看到顶部栏和底部导航。有什么想法发生了什么?

android android-fragments android-framelayout bottomnavigationview fragmentmanager
2个回答
0
投票

尝试将活动布局中的<include>标签的android:layout *属性修改为:

<include
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

0
投票

事实证明我添加的一个片段有一个函数,它的作用是隐藏这两个元素,因此在我将片段添加到片段管理器后,无论该片段是否处于活动状态,它们都是不可见的。

© www.soinside.com 2019 - 2024. All rights reserved.