简体   繁体   中英

Android header - how do I make it stay on top of the page and not on top of screen?

I made a nice android header. The only problem with it is that whenever the user scrolls down, the header follows the scroll. So the header ends up scrolling and remains on the screen.

What I want to happen is for it to stay glued to the very top of the page. Is that possible?

Here is my header:

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

    <!--     android:textColor="@color/white"
    -->

 <Button android:id="@+id/home_header"
  android:layout_width="0dp" 
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:textColor="@color/white"
  android:text="Home"
  android:background="@drawable/ic_menu"
  android:textSize="11dp"
/>

<Button android:id="@+id/businesses_header"
  android:layout_width="0dp" 
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:textSize="11dp"
  android:text="Businesses"
  android:textColor="@color/white"
    android:background="@drawable/ic_menu"
  android:layout_toRightOf="@+id/home_header"
 />   

 <Button android:id="@+id/learn_header"
  android:layout_width="0dp" 
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="Learn"
  android:textColor="@color/white"
  android:textSize="11dp"
  android:background="@drawable/ic_menu"
  android:layout_toRightOf="@+id/businesses_header"
 />   


<Button android:id="@+id/questions_header"
 android:layout_width="0dp" 
 android:layout_height="wrap_content"
 android:textColor="@color/white"
 android:layout_weight="1"
 android:text="Questions"
 android:textSize="11dp"
   android:background="@drawable/ic_menu"
android:layout_toRightOf="@+id/learn_header"  />  



 <Button android:id="@+id/extra_help_header"
  android:layout_width="0dp" 
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:textColor="@color/white"
  android:text="Premium"
  android:textSize="11dp"
  android:background="@drawable/ic_menu"
  android:layout_toRightOf="@+id/questions_header"
 />   
</LinearLayout>

Thank you

try this

<Button android:id="@+id/home_header"
  android:layout_width="0dp" 
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:textColor="@color/white"
  android:text="Home"
  android:background="@drawable/ic_menu"
  android:textSize="11dp"
/>

 <Button android:id="@+id/learn_header"
  android:layout_width="0dp" 
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:text="Learn"
  android:textColor="@color/white"
  android:textSize="11dp"
  android:background="@drawable/ic_menu"
  android:layout_toRightOf="@+id/businesses_header"
 />   


<Button android:id="@+id/questions_header"
 android:layout_width="0dp" 
 android:layout_height="wrap_content"
 android:textColor="@color/white"
 android:layout_weight="1"
 android:text="Questions"
 android:textSize="11dp"
   android:background="@drawable/ic_menu"
android:layout_toRightOf="@+id/learn_header"  />  



 <Button android:id="@+id/extra_help_header"
  android:layout_width="0dp" 
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:textColor="@color/white"
  android:text="Premium"
  android:textSize="11dp"
  android:background="@drawable/ic_menu"
  android:layout_toRightOf="@+id/questions_header"
 />   
 <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/Layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical" >

Here add remaining part of the xml code

</LinearLayout>

The first thing you should do is change the height in your header to wrap_content:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:orientation="horizontal">

Then save the header layout in its own layout file (header.xml). Then use include statements whenever you want to use your header.

Here the header stays on top:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <include android:id="+@id/header" layout="@layout/header" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:id="+@id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!-- put content here -->
        </LinearLayout>
    </ScrollView>
</LinearLayout>

Here it scrolls with the content:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <include android:id="+@id/header" layout="@layout/header" />

            <!-- put content here -->
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM