【问题标题】:How to set Header , Scrollable content and Footer? in Android如何设置页眉、可滚动内容和页脚?在安卓中
【发布时间】:2012-12-05 23:04:37
【问题描述】:

我想让中间部分可以滚动。我不知道..这是我的 xml 大纲。我想在可滚动区域插入这个 xml 大纲。

注意:页眉和页脚必须保持不变。只有中间区域需要滚动

【问题讨论】:

    标签: android android-layout header android-linearlayout footer


    【解决方案1】:

    更简单的方法是使用:

    <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=".MainActivity" >
    
        <LinearLayout
            android:id="@+id/headerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" >
        </LinearLayout>
    
        <ScrollView
            android:id="@+id/scrollablContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/footerView"
            android:layout_below="@+id/headerView" >
    
            <LinearLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/footerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 它只是一个示例,您必须将您的小部件添加到特定的 groupview,例如您的标题必须添加到 LinearLayout headerView。
    【解决方案2】:

    您必须将外部容器从LinearLayout替换为RelativeLayout,然后为header ImageView设置alignParentTop=true,为footer ImageView设置alignParentBottom=true,并设置layout_above="@+id/imageViewFooter", layout_below="@ ScrollView 的 +id/imageViewHeader"。像这样:

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/imageViewHeader"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="false"
            android:layout_alignParentTop="false"
            android:scaleType="fitXY"
            android:src="@drawable/square" />
    
        <ImageView
            android:id="@+id/imageViewFooter"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
    
            android:scaleType="fitXY"
            android:src="@drawable/square" />
    
        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageViewFooter"
    
            android:layout_below="@+id/imageViewHeader" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
    
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
    
                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
    
                <Button
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
            </LinearLayout>
        </ScrollView>
    
    </RelativeLayout>
    

    【讨论】:

    • 如何设置这个android:scaleType="fitXY"android:src="@drawable/square"
    • 我的坏!我明白了.. fitXY 是什么意思
    • fitXY 只是告诉 ImageView 图像必须符合规定的尺寸,而不考虑比例。
    • 当键盘弹出页脚视图时也向上滚动如何禁用
    【解决方案3】:

    您可能想要包含页眉和页脚的 listView。

    你见过this?

    另见this one.

    还有this one.

    希望这对您有所帮助。如果没有,请告诉我。

    享受编码。 :)

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      相关资源
      最近更新 更多