我在android中尝试了底部导航视图。但它正在根据我在它上面的片段改变它的位置。如何解决这个问题?
活动主.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"
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>
片段主页.xml:
<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"
android:orientation="vertical"
android:gravity="center"
android:layout_margin="10dp"
android:clipToPadding="false"
tools:context="com.d.wordsearch.HomeFragment">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_edit_text"
android:maxLines="1"
android:hint="@string/hint_search_field"
/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/word_search_button"
android:text="@string/button_word_search"
android:background="@drawable/ripple"
style="@style/ButtonAppearance"
android:layout_marginRight="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/other_search_button"
android:text="@string/button_other_search"
android:background="@drawable/ripple"
style="@style/ButtonAppearance"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</LinearLayout>
为了检查是否发生这种情况,我在其中一个片段中添加了一些文本视图。
片段首选项.xml:
<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"
android:orientation="vertical"
tools:context="com.d.wordsearch.PreferencesFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences" />
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences"
android:layout_marginTop="200dp"
/>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences" />
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences" />
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences"
android:layout_marginTop="200dp"
/>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Preferences"
android:layout_marginTop="200dp"
/>
</LinearLayout>
这就是我在片段之间切换时发生的情况: