[英]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.