框架布局接管屏幕并隐藏底部导航和工具栏
Posted
技术标签:
【中文标题】框架布局接管屏幕并隐藏底部导航和工具栏【英文标题】:Frame layout takes over screen and hides bottom navigation and toolbar 【发布时间】:2019-08-23 05:28:16 【问题描述】:我正在尝试将当前使用 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_
android:layout_
tools:context=".FeedActivity"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_
android:layout_
android:theme="@style/MyActionBar"
android:minHeight="?attr/actionBarSize"
android:id="@+id/my_toolbar"/>
<include
layout="@layout/content_main"
android:layout_
android:layout_/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/feed_bottom_nav"
android:layout_
android:layout_
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_
android:layout_
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()
我可以再次看到顶部栏和底部导航。有什么想法吗?
【问题讨论】:
【参考方案1】:尝试在你的活动布局中修改<include>
标签的android:layout*属性:
<include
layout="@layout/content_main"
android:layout_
android:layout_
android:layout_weight="1" />
【讨论】:
还是不行,工具栏和底部导航都不可见【参考方案2】:原来我添加的一个片段有一个功能,它的工作是隐藏这两个元素,所以在我将该片段添加到片段管理器之后,无论该片段是否处于活动状态,它们都是不可见的不是。
【讨论】:
以上是关于框架布局接管屏幕并隐藏底部导航和工具栏的主要内容,如果未能解决你的问题,请参考以下文章