使用ScrollView实现BottomNavigationView

时间:2018-05-25 03:34:20

标签: java android android-studio

我正在尝试实施BottomNavigationView并在内容中ScrollView。一切正常,但是当BottomNavigationView使用ScrollView打开FrameLayout时,效果不佳

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.panel.roubook.activities.NewBook">


    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_navigation"
        android:layout_alignParentTop="true">

    </FrameLayout>

    <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/colorAccent"
        app:itemIconTint="@color/colorPrimary"
        app:itemTextColor="@color/colorPrimary"
        app:menu="@menu/navigation_items" />

</RelativeLayout>

这是我的框架

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="72dp"
            android:layout_marginBottom="5dp"
            android:layout_gravity="center_horizontal" />

        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp">
            <EditText android:id="@+id/txtEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/user_email_text" />
        </android.support.design.widget.TextInputLayout>

        <!-- Password Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp">
            <EditText android:id="@+id/txtPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="@string/user_password"/>
        </android.support.design.widget.TextInputLayout>

        <!-- Signup Button -->
        <android.support.v7.widget.AppCompatButton
            android:id="@+id/btn_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp"
            android:padding="12dp"
            android:text="@string/user_btn_login"/>

        <TextView android:id="@+id/link_register"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp"
            android:text="@string/user_link_to_register"
            android:gravity="center"
            android:textSize="16dip"/>

        <TextView android:id="@+id/link_forgot"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp"
            android:text="@string/user_link_forgot_password"
            android:gravity="center"
            android:textSize="16dip"/>


        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp">
            <EditText android:id="@+id/txtEmail2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/user_email_text" />
        </android.support.design.widget.TextInputLayout>

        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp">
            <EditText android:id="@+id/txtEmail3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/user_email_text" />
        </android.support.design.widget.TextInputLayout>


        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp">
            <EditText android:id="@+id/txtEmail4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/user_email_text" />
        </android.support.design.widget.TextInputLayout>


        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp">
            <EditText android:id="@+id/txtEmail5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/user_email_text" />
        </android.support.design.widget.TextInputLayout>

        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp">
            <EditText android:id="@+id/txtEmail6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/user_email_text" />
        </android.support.design.widget.TextInputLayout>

    </LinearLayout>
</ScrollView>

在我的MainActivity.java

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getFragmentManager();
        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.action_explorer:
                                return true;

                            case R.id.action_ready:
                                return true;

                            case R.id.action_account:
                                fragmentTransaction.replace(android.R.id.content , new Account());
                                fragmentTransaction.commit();
                                return true;

                        }
                        return true;
                    }
                });
    }

当我按下帐户时,屏幕更改为帐户,但滚动视图覆盖了BottomNavigationView,并且无法使用BottonNavigationView的其他按钮

enter image description here

抱歉我的英文。

2 个答案:

答案 0 :(得分:1)

android.R.id.content替换为R.id.content,因为android.R.id.content不是您在XML中定义的FrameLayout的ID,而是其他视图的ID。

 bottomNavigationView.setOnNavigationItemSelectedListener(
                    new BottomNavigationView.OnNavigationItemSelectedListener() {
                        @Override
                        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.action_explorer:
                                    return true;

                                case R.id.action_ready:
                                    return true;

                                case R.id.action_account:
                                    fragmentTransaction.replace(R.id.content , new Account());
                                    fragmentTransaction.commit();
                                    return true;

                            }
                            return true;
                        }
                    });

答案 1 :(得分:1)

android.R.id.content替换为R.id.content

android.R.id.content不是您在activity_main.xml中设置的资源ID。