带滚动的 Android 布局

标签 android android-layout

如何创建这样的 Android UI

enter image description here

01 和 02 布局高度应为设备高度的 1/3。默认布局应该显示黑色区域,向下滚动它应该显示 01 和黑色布局的 2/3。

  1. 如果主视图显示01 和黑色布局的 2/3 并且用户向上滚动 那么它应该导航到主布局(黑色布局)

最佳答案

您需要创建一个布局,如果您滚动,它会在必要时移开,这与简单的固定页眉和页脚不同。如果正文的大小超过了可用空间,我们希望布局表现得像这样

enter image description here

Notice how, now that the screen’s size is not enough to display all of our content, the footer and the header are no longer anchored and respond to scrolling, without overlapping the body.

So, how do we get there? We use LinearLayout‘s layout_weight behavior to ensure that the body area will always expand to be at least as long as the remaining sandwiched space between the header and the footer. If the content’s shorter, it expands until it reaches the footer’s top; if it’s longer, it pushes the footer down.

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

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

  <!-- HEADER -->
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
  />

  <!-- BODY -->
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="vertical"
  />

  <!-- FOOTER -->
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
  />
</LinearLayout>
</ScrollView>

此外,您还希望 ListView 中的内容对齐到顶部,您可以在此处查看此教程: http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/

来源: http://blogactivity.wordpress.com/2012/02/22/smart-headers-and-footers-in-scrollviews/
编辑:更新为可粘贴代码并添加了包含捕捉信息的链接。

关于带滚动的 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20997411/

相关文章:

android - 如何在 EditText 中放置按钮

android - 打开相机前显示对话框

java - Android 二进制布局膨胀异常 : ClassNotFoundException: Didn't find class "android.view.layout" on path:

android - 更好地使用 Android Studio 的 logcat,永久关注单个进程?

android - 在平板电脑上模拟较小的屏幕?

java - Android 多次滑动 View

android - 嵌套 fragment 内的 Recyclerview 未显示

android - MPAndroidChart:删除 BarChart 中 x 轴标签的起始填充

android - 通过触摸输入在位图上绘制透明线

java - Edittext 和 ProgressDialog 均不出现