java - 使用 ScrollView 实现 BottomNavigationView

标签 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;
                    }
                });
    }

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

enter image description here

抱歉我的英语不好。

最佳答案

android.R.id.content 替换为 R.id.content 因为 android.R.id.content 不是您的 ID您在 XML 中定义的 FrameLayout,它是其他 View 的 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;
                        }
                    });

关于java - 使用 ScrollView 实现 BottomNavigationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50521133/

相关文章:

android - 如何使用 android studio 开发旧版 Android 应用程序(api>=10)?

java - 在不同屏幕尺寸上运行应用程序时,谷歌地图标记会变得更大

java - 工作区中的项目在其构建路径上有一个 jar

c# - Xamarin.Android 中基于 RESX 的本地化

android - 为什么 Android Studio 2.0 不支持 Perforce?

Android Studio Gradle 无法解析符号 'applicationVariants'

java - 带有背景图像的 JTextArea 的内部填充

java - 从 ActiveMQ 队列中获取消息而不出队

android - 如何防止 Volley Library 在完成请求后在 Log 中显示链接?

android - Android 8 和 9 未授予可选的危险权限 READ_PHONE_STATE?