如何在Android中的布局顶部修复App Bar和Edittext

时间:2015-12-18 12:08:15

标签: android android-layout android-appbarlayout

我有这个布局:

<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>

它在屏幕上看起来像这样:

enter image description here

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

5 个答案:

答案 0 :(得分:3)

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>

答案 1 :(得分:2)

只需将EditText和ImageButton移出scrollView即可。然后在它下面启动ScrollView。

答案 2 :(得分:2)

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 

答案 3 :(得分:2)

    <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>

答案 4 :(得分:2)

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>