Android 如何让 App Bar 和 Edittext 固定在布局的顶部

How to make App Bar and an Edittext fixed at the top of the layout in Android

我有这样的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">




    <LinearLayout 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"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_show" tools:context="com.example.bimpc1.myapp.ShowActivity"
        android:background="@color/backgrndclr"
        >


        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            xmlns:android="http://schemas.android.com/apk/res/android" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">




            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/et_word"
                android:ems="14"
                android:hint="Search"
                android:drawableBottom="@color/bottomcolor"/>

            <ImageButton
                android:id="@+id/btn_bring"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:src="@drawable/search"
                android:background="@color/buttonbckgrndclr"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:paddingBottom="5dp"
                />

        </LinearLayout>
<!-- And lots of other view elements -->
    </LinearLayout>
</ScrollView>

屏幕上看起来像这样:

我希望将该栏和搜索编辑文本和搜索按钮固定在屏幕顶部。我的意思是当用户向下滚动时它们不应该消失。我怎样才能做到这一点?我用谷歌搜索了我的问题,但找不到解决方案。谢谢

只需将 EditText 和 ImageButton 移出 scrollView。并在其下方启动您的 ScrollView。

ScrollView 移到您的两个元素下方:

<LinearLayout 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"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_show" tools:context="com.example.bimpc1.myapp.ShowActivity"
    android:background="@color/backgrndclr"
    >


    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        xmlns:android="http://schemas.android.com/apk/res/android" />
    <View
        android:layout_width="fill_parent"
        android:layout_height="30dp">
    </View>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/et_word"
            android:ems="14"
            android:hint="Search"
            android:drawableBottom="@color/bottomcolor"/>

        <ImageButton
            android:id="@+id/btn_bring"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:src="@drawable/search"
            android:background="@color/buttonbckgrndclr"
            android:paddingRight="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="5dp"
            />

    </LinearLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        xmlns:app="http://schemas.android.com/apk/res-auto">    

        <!-- And lots of other view elements -->
        </ScrollView>
</LinearLayout>
You can put it that Out of the ScrollView and then start the code like

 <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="match_parent"
tools:context="com.iconapp.Activity">

      <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/et_word"
            android:ems="14"
            android:hint="Search"
            android:drawableBottom="@color/bottomcolor"/>

        <ImageButton
            android:id="@+id/btn_bring"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:src="@drawable/search"
            android:background="@color/buttonbckgrndclr"
            android:paddingRight="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="5dp"
            />


            <ScrollView -- 
             -- >
     - -- - --  Write Your design code here 

    <LinearLayout 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="fill_parent"
        android:fillViewport="true"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            xmlns:android="http://schemas.android.com/apk/res/android" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/et_word"
                android:ems="14"
                android:hint="Search"
                android:drawableBottom="@color/bottomcolor"/>

            <ImageButton
                android:id="@+id/btn_bring"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:src="@drawable/search"
                android:background="@color/buttonbckgrndclr"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:paddingBottom="5dp"
                />

        </LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            PUT all ur other views here

        </LinearLayout>


    </ScrollView>
        </LinearLayout>
Use this code and always use `android:layout_height="wrap_content"` of child layout of scroll layout and child should be only one

    <LinearLayout 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="fill_parent"
        android:fillViewport="true"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            xmlns:android="http://schemas.android.com/apk/res/android" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/et_word"
                android:ems="14"
                android:hint="Search"
                android:drawableBottom="@color/bottomcolor"/>

            <ImageButton
                android:id="@+id/btn_bring"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:src="@drawable/search"
                android:background="@color/buttonbckgrndclr"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:paddingBottom="5dp"
                />

        </LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

                  </LinearLayout>


    </ScrollView>
        </LinearLayout>