简体   繁体   English

Android标题 - 如何使其保持在页面顶部而不是屏幕顶部?

[英]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. 我做了一个漂亮的android头。 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 这里添加xml代码的剩余部分

</LinearLayout>

The first thing you should do is change the height in your header to wrap_content: 您应该做的第一件事是将标题中的高度更改为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). 然后将标题布局保存在自己的布局文件(header.xml)中。 Then use include statements whenever you want to use your header. 然后,只要您想使用标题,就使用include语句。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android studio中的布局问题。 如何使我的相对布局覆盖整个屏幕,而不会卡在左上方? - Issue with layout in android studio. How do I get my relative layout to cover the entire screen and not stay stuck in the top left? 如何让通知保持在最前面? - How to make notification stay on top? Android Admob 4.3.1如何使adView在屏幕顶部显示? - Android Admob 4.3.1 how do I make the adView show on top of the screen? 如何使ListFragment仅占据屏幕的上半部分 - How do I make a ListFragment occupy only the top half of the screen 如何让Android模拟器始终在macOS中保持领先 - How to make Android emulator always stay on top in macOS 如何使活动窗口始终在最前面 - How to make an activity window stay always on top 滚动列表视图时,如何使单个标题停留在屏幕顶部? - How can I make a single header stick to the top of the screen when scrolling a listview? Android:CoordinatorLayout-如何在顶部屏幕显示修复标题视图? - Android : CoordinatorLayout - How to have a fix header view at the top screen? 如何让工具栏保持在屏幕顶部而不是显示在整个屏幕上? - How can I have a toolbar stay at top of screen instead of showing up on the whole screen? 如何使自定义视图保持在屏幕上 - How do I make a custom view stay in the screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM