使用 BottomNavigationView 时片段的正确显示存在问题。片段数据位于底部导航视图下。我认为,在这里,您很可能需要在活动中做点什么。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".actyvities.MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu"/>
</android.support.constraint.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_event -> {
val fragment =
EventFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_person -> {
val fragment =
PersonFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_reports -> {
val fragment =
ReportFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_settings -> {
val fragment =
SettingsFragment()
addFragment(fragment)
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
val fragment = EventFragment()
addFragment(fragment)
}
private fun addFragment(fragment: Fragment){
supportFragmentManager.beginTransaction().replace(R.id.container, fragment, fragment.javaClass.getSimpleName())
.commit()
}
其中一个片段:
class PersonFragment: Fragment() {
val list = ArrayList<Person>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val myView = inflater.inflate(R.layout.fragment_persons,container,false)
loadPersonList()
showPersonList(myView)
return myView
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
fun showPersonList(view: View){
val myListView = view.findViewById<ListView>(R.id.lvPerson)
myListView.adapter = PersonAdapter(
layoutInflater.context,
listOfPerson = list
)
}
fun loadPersonList(){
list.add(Person("Иванов Иван"))
list.add(Person("Петров Петр"))
list.add(Person("Сергеев Сергей"))
list.add(Person("Иванов Иван"))
list.add(Person("Петров Петр"))
list.add(Person("Сергеев Сергей"))
list.add(Person("Иванов Иван"))
list.add(Person("Петров Петр"))
list.add(Person("Сергеев Сергей"))
list.add(Person("Иванов Иван"))
list.add(Person("Петров Петр"))
list.add(Person("Сергеев Сергей"))
list.add(Person("Иванов Иван"))
list.add(Person("Петров Петр"))
list.add(Person("Сергеев Сергей"))
}
fragment_persons.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:padding="3dp">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/team"
android:textSize="18sp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="32"/>
</LinearLayout>
<ListView
android:id="@+id/lvPerson"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:clickable="true"
app:backgroundTint="@color/colorPrimary"
android:layout_marginBottom="50dp"
app:fabSize="normal"
app:srcCompat="@drawable/ic_person_add_black_24dp"/>
</android.support.design.widget.CoordinatorLayout>
在用于测试的片段中,我放入了一个列表,当显示时,列表和 fab 中的人员会转到 BottomNavigationView。怎么修?提前致谢。
在您的情况下,您使用父视图作为容器,其中还包含底部导航:
借助帮助进行导航的方式与
BottomNavigationView
您所介绍的有所不同。为了能够更改片段,使用了容器。这是一个示例标记:将来会用到
FrameLayout
。为了使片段的所有内容都在屏幕上而不是在导航栏下,请使用margin_bottom
:也就是说,这个标签的逻辑是将容器提升到您的
Bottom_Navigation_View
. 查看容器的标记,如果不margin_bottom
存在,请添加它。但是,最好将容器作为视图添加到父布局,而不是将父布局用作容器。因为如果给父视图加上指定的边距,那么一切都会随着bottomNavigationView
.