使用ListView init布局的BottomNavigationView

时间:2017-11-28 16:27:30

标签: android xml listview

我的活动有两个布局XML文件。 activity_main.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"
    android:background="#f1f3fa">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/actionBarSize"
            android:background="@color/colorPrimary"
            android:title="@string/title_activity_contact"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml 是:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/search_linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <SearchView
                    android:id="@+id/searchView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/search_linear"
            android:orientation="vertical">

                <TextView
                    android:id="@+id/search_start_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="10dp"
                    android:background="@drawable/search_box"
                    android:lineSpacingExtra="5dp"
                    android:padding="12dp"
                    android:text="some text"
                    android:textColor="#333"
                    android:textSize="14sp" />

                <ListView
                    android:id="@+id/search_list"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginBottom="7dp"
                    android:layout_weight="1"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp">

                </ListView>

        </LinearLayout>


</RelativeLayout>

没有 ListView 当我点击 SearchView 时,键盘会正确打开。但是当键盘打开时 ListView BottomNavigationView 会上升。

如何解决该问题以避免 BottomNavigationView 上升?

1 个答案:

答案 0 :(得分:0)

首先,将以下内容添加到AndroidMainfest.xml:

   <activity
android:name="com.companyname.applicationname"
android:windowSoftInputMode="adjustPan">

使您的content_main.xml成为ScrollView的子级。而不是相对布局。由于scrollview只能有一个直接子项,因此只需将所有现有的xml代码放在scrollview中。

content_main.xml

 <ScrollView 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="fill_parent"
        android:layout_height="fill_parent"
        android:isScrollContainer="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/search_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/search_linear"
        android:orientation="vertical">

            <TextView
                android:id="@+id/search_start_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="@drawable/search_box"
                android:lineSpacingExtra="5dp"
                android:padding="12dp"
                android:text="some text"
                android:textColor="#333"
                android:textSize="14sp" />

            <ListView
                android:id="@+id/search_list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="7dp"
                android:layout_weight="1"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

            </ListView>

    </LinearLayout>


    </RelativeLayout>

</ScrollView>