如何使底部导航视图在android中保持在某个位置?
我尝试了android中的底部导航视图。但它正在根据我在上面展示的片段改变它的位置。如何解决这个问题 activity_main.xml:如何使底部导航视图在android中保持在某个位置?,android,xml,android-fragments,bottomnavigationview,Android,Xml,Android Fragments,Bottomnavigationview,我尝试了android中的底部导航视图。但它正在根据我在上面展示的片段改变它的位置。如何解决这个问题 activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andr
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.d.wordsearch.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:id="@+id/fragmentContainer"
/>
<android.support.design.widget.BottomNavigationView
xmlns:design="http://schema.android.com/apk/res/android.support.design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomMenu"
android:layout_weight="1"
android:layout_gravity="bottom"
android:paddingTop="20dp"
android:layout_alignParentBottom="true"
app:menu="@menu/my_navigation_items"
/>
</LinearLayout>
错误的权重分配给布局使用此类型,每个屏幕的权重比率应为100%,然后将权重9设置为Framlayout或使用RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.d.wordsearch.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:id="@+id/fragmentContainer"
/>
<android.support.design.widget.BottomNavigationView
xmlns:design="http://schema.android.com/apk/res/android.support.design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomMenu"
android:layout_weight="1"
android:layout_gravity="bottom"
android:paddingTop="20dp"
android:layout_alignParentBottom="true"
app:menu="@menu/my_navigation_items"
/>
</LinearLayout>
如果使用RelativeLayout,则使用此类型
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content Container -->
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/bottom_navigation_main" />
</RelativeLayout>
只需将FrameLayout
和BottomNavigationView
的高度设置为0dp
android:layout_height="0dp"
其他一切似乎都很好。使用RelativeLayout而不是LinearLayout,并保持底部栏对齐。不帮忙可能会给你更多的信息,明白了。谢谢
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content Container -->
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/bottom_navigation_main" />
</RelativeLayout>
android:layout_height="0dp"