1

我在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>

这就是我在片段之间切换时发生的情况:

家庭片段

首选项片段

4

2 回答 2

1

错误的权重分配给你的布局使用这种类型,权重比例应该是 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>
于 2017-01-27T07:25:01.970 回答
0

只需设置高度FrameLayoutBottomNavigationView0dp

android:layout_height="0dp"

其他一切似乎都很好。

于 2017-01-27T07:44:26.403 回答