【问题标题】:How to make a search box not scrolling over scrollview Android如何使搜索框不滚动滚动视图 Android
【发布时间】:2016-08-27 14:25:52
【问题描述】:

在我的主屏幕中,我想添加带有导航抽屉的搜索框,但我无法在滚动视图上设置不可滚动的编辑文本。

这是我的代码

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#ecedf1">






    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_200sdp" />



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/_200sdp"
           >
            <EditText
                android:layout_width="@dimen/_280sdp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/_10sdp"
                android:layout_height="@dimen/_40sdp"
                android:text="fdgsdfhsfhsfghhsfhsfhf"
                android:background="@drawable/customedittext"/>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/viewPagerIndicator"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_marginTop="@dimen/_180sdp"
            android:gravity="center">

            <LinearLayout
                android:id="@+id/viewPagerCountDots"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:orientation="horizontal" />

        </RelativeLayout>

        <com.information.aanjana.aanjanainformation.NonScrollGridView
            android:id="@+id/gv_android"
            android:layout_width="@dimen/_299sdp"
            android:layout_height="match_parent"
            android:horizontalSpacing="8dp"
            android:layout_centerHorizontal="true"
            android:numColumns="2"
            android:paddingLeft="@dimen/_4sdp"
            android:paddingRight="@dimen/_4sdp"
            android:layout_marginTop="@dimen/_220sdp"
            android:verticalSpacing="8dp"/>

    </RelativeLayout>

    </ScrollView>


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

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

我想要一个带有导航抽屉的编辑文本,它应该是粘性的,并且不会随着布局的其余部分滚动。我怎样才能实现它。

下面的截图解释了我想要的:

【问题讨论】:

  • 嗨,你的 UI 看起来很酷 :)
  • @GKTHEBOSS 谢谢,但你能帮帮我吗? :)
  • 你能告诉我你的 searchView 的 id 是你想保留在滚动部分之外的吗?
  • @himanshu1496 我没有给它 id 但是用这个参数检查edittext android:text="fdgsdfhsfhsfghhsfhsfhf"
  • 好的,知道了。很快就会发布我的答案。

标签: android xml android-layout android-scrollview


【解决方案1】:

试试我下面的代码:

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#ecedf1">





<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">


        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_200sdp" />

            <RelativeLayout
                android:id="@+id/viewPagerIndicator"
                android:layout_width="match_parent"
                android:layout_height="25dp"
                android:layout_marginTop="@dimen/_180sdp"
                android:gravity="center">

                <LinearLayout
                    android:id="@+id/viewPagerCountDots"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:gravity="center"
                    android:orientation="horizontal" />

            </RelativeLayout>

            <com.information.aanjana.aanjanainformation.NonScrollGridView
                android:id="@+id/gv_android"
                android:layout_width="@dimen/_299sdp"
                android:layout_height="match_parent"
                android:horizontalSpacing="8dp"
                android:layout_centerHorizontal="true"
                android:numColumns="2"
                android:paddingLeft="@dimen/_4sdp"
                android:paddingRight="@dimen/_4sdp"
                android:layout_marginTop="@dimen/_220sdp"
                android:verticalSpacing="8dp"/>

        </RelativeLayout>

    </ScrollView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/_200sdp"
        android:layout_alignParentTop="true"
        >
        <EditText
            android:layout_width="@dimen/_280sdp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_height="@dimen/_40sdp"
            android:text="fdgsdfhsfhsfghhsfhsfhf"
            android:background="@drawable/customedittext"/>
    </RelativeLayout>

</RelativeLayout>




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

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

这里我唯一改变的是将editText 保留在scrollView 之外。

【讨论】:

  • 效果很好..但是你能告诉我如何在搜索框上设置导航抽屉吗?
  • 一种方法是在search edit text 的左侧设置一个button 并在该button 上设置一个click listener,只要单击它就打开和关闭drawer .但这是另一个问题,如果您满意,请接受答案。
【解决方案2】:

你为什么不试着把它放在 ScrollView 之外。然后用一个RelativeLayout覆盖它,并将重力设置为top。我现在无法运行它,但在 xml 中应该看起来像这样

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#ecedf1">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_200sdp" />

    <RelativeLayout
        android:id="@+id/viewPagerIndicator"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_marginTop="@dimen/_180sdp"
        android:gravity="center">

        <LinearLayout
            android:id="@+id/viewPagerCountDots"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:orientation="horizontal" />

    </RelativeLayout>

    <com.information.aanjana.aanjanainformation.NonScrollGridView
        android:id="@+id/gv_android"
        android:layout_width="@dimen/_299sdp"
        android:layout_height="match_parent"
        android:horizontalSpacing="8dp"
        android:layout_centerHorizontal="true"
        android:numColumns="2"
        android:paddingLeft="@dimen/_4sdp"
        android:paddingRight="@dimen/_4sdp"
        android:layout_marginTop="@dimen/_220sdp"
        android:verticalSpacing="8dp"/>

</RelativeLayout>

</ScrollView>

<!-- Here we add the layout outside the ScrollView -->

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/_200sdp"
        android:gravity="top"> <!-- GRAVITY ADJUST -->

        <EditText
            android:layout_width="@dimen/_280sdp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_height="@dimen/_40sdp"
            android:text="fdgsdfhsfhsfghhsfhsfhf"
            android:background="@drawable/customedittext"/>
    </RelativeLayout>



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

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

希望它运行顺利并且很有帮助。

【讨论】:

  • 不工作的相对布局落后于滚动视图内容
  • 只需先放置滚动视图,然后放置编辑文本布局。已编辑。
  • 你能告诉我如何在搜索框上设置导航抽屉,就像我添加的上图一样吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多