【问题标题】:Navigation bar with scroll view android带有滚动视图android的导航栏
【发布时间】:2016-11-22 15:51:58
【问题描述】:

这是我使用导航栏和滚动视图的布局。

<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:fitsSystemWindows="true"
tools:context=".AccountActivity">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                app:elevation="0dp" />

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

        <!--content of account activity-->

        <ScrollView
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            >

            <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:id="@+id/activity_setup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".AccountActivity">

                <ImageView
                    android:layout_width="match_parent"
                    android:background="@drawable/listgrad"
                    android:id="@+id/imageView"
                    android:layout_height="100dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:text="Name"
                    android:layout_below="@+id/profilepic"
                    android:id="@+id/accountnamefield"
                    android:textSize="18sp"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Recently Match"
                    android:id="@+id/recentMatchTitle"
                    android:textSize="18sp"
                    android:layout_below="@+id/accountnamefield"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <android.support.v7.widget.RecyclerView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/recentMatchProductList"
                    android:layout_below="@+id/recentMatchTitle"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true">
                </android.support.v7.widget.RecyclerView>

                <ImageButton
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    app:srcCompat="@mipmap/ic_account_circle_white_36dp"
                    android:id="@+id/profilepic"
                    android:background="@drawable/round_button"
                    android:layout_marginTop="21dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />

            </RelativeLayout>

        </ScrollView>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/drawer" />

</android.support.v4.widget.DrawerLayout>

我正在使用带有导航抽屉的滚动视图。为什么我打开和关闭导航抽屉然后视图将滚动到底部位置?我可以将它移到顶部或保持相同的位置吗?请给我一些帮助。

【问题讨论】:

    标签: android scrollview android-scrollview navigation-drawer


    【解决方案1】:

    首先删除android:inputType="textPersonName"TextView 部分。然后 将android:scrollbars="vertical" 添加到您的ScrollView 中。对我来说效果很好。

    在这里我将发布我用于测试的代码,ScrollView 在使用 NavigationDrawer 时保持相同的位置

    <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:fitsSystemWindows="true"
    tools:context=".MainActivity">
    
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <!-- your content layout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
    
            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:elevation="0dp">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="?attr/actionBarSize"
                    app:elevation="0dp" />
    
            </android.support.design.widget.AppBarLayout>
    
            <!--content of account activity-->
    
            <ScrollView
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:scrollbars="vertical"
                >
    
                <LinearLayout 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:id="@+id/activity_setup"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    tools:context=".MainActivity">
    
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/bla"
                        android:id="@+id/accountnamefield"
                        android:textSize="18sp"
                        android:layout_centerVertical="true"
                        android:layout_centerHorizontal="true"
                        />
    
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Recently Match"
                        android:id="@+id/recentMatchTitle"
                        android:textSize="18sp"
                        android:layout_below="@+id/accountnamefield"
                        android:layout_centerVertical="true"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true" />
    
    
    
                </LinearLayout>
    
            </ScrollView>
    
        </LinearLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 感谢您的回答。但是,我无法解决我的问题。我认为滚动视图受回收站视图的影响,所以它会移到底部。
    【解决方案2】:

    你的布局有很多问题:

    android:layout_alignParentStart="true" - 与左(或 RTL 的右)相同 android:layout_alignParentEnd="true" - 与 right 相同(或 留给 RTL)

    同时声明start和left是错误的

    在 ScrollView 中使用回收器视图 - 不好的做法,永远不会起作用。您可以使用嵌套滚动视图 (RecyclerView inside ScrollView is not working),但更好的解决方案是将您想要的内容放在滚动视图中(作为页眉和页脚)。

    我认为没有办法衡量您的布局。尝试弄清楚 android 是如何绘制视图的 (https://medium.com/@britt.barak/measure-layout-draw-483c6a4d2fab#.y7nd8gyql)。

    附:你的布局有更多的问题,写一个新的比指出所有问题更容易。

    【讨论】:

    • android:layout_alignParentStart="true" 和 android:layout_alignParentEnd="true" 是在我使用 xml 拖动视图时自动生成的。我会看看那个网站,但我仍然不太擅长在一项活动中处理这么多视图。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-04-12
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多