【问题标题】:How to make a static button under a ScrollView?如何在 ScrollView 下制作静态按钮?
【发布时间】:2011-11-25 15:46:57
【问题描述】:

我有以下 XML。我正在尝试在我的 ScrollView 下方制作一个静态按钮。我尝试设置权重,将按钮设置在滚动视图下方等。有人可以给我一种方法,让我可以让按钮保持在底部并且滚动视图只占据屏幕中间吗?

<?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="wrap_content">



    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/menuButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/newItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Items" />
        <Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/categories"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Categories" />

    </LinearLayout>


    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/contentScroller"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/menuButtons">

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


            <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1" >


                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip" >
                    <ImageView
                        android:src="@drawable/thumbdrive"/>"
                    <TextView
                        android:layout_column="1"            
                        android:text="Thumb Drives"            
                        android:padding="3dip" 
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <ImageView
                        android:src="@drawable/laptop"/>
                    <TextView
                        android:layout_column="1"
                        android:text="Laptops"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <ImageView
                        android:src="@drawable/sdcard"/>
                    <TextView
                        android:layout_column="1"
                        android:text="SD Cards"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
                <TableRow android:layout_marginTop="5dip"
                          android:layout_marginBottom="5dip">
                    <TextView
                        android:layout_column="1"
                        android:text="Other"
                        android:padding="3dip"
                        android:textSize="20dip"/>
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Submit New Item"
        android:layout_below="@id/contentScroller"/>

</RelativeLayout>

【问题讨论】:

  • 我昨天解决了完全相同的问题,但我发现了另一个技巧,只是出于好奇,你能告诉我横向模式下的视觉效果如何?谢谢
  • 横向看起来不错。

标签: android


【解决方案1】:

我知道这已经很晚了,但有更好的解决方案。 RelativeLayout 方法以及将按钮与相对布局底部对齐的问题在于,它强制布局高度与 fill_parent(或 match_parent)基本相同。

正确的做法如下:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">
            <!-- Your Scrollview content goes here -->
    </ScrollView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Button Text Goes Here" />
</LinearLayout>

关键是将高度设置为 0,然后将其布局权重设为 1...从阅读布局调整过程中可以看出,将其大小设置为 0 并且布局权重会导致它推迟调整视图的大小,直到它处理完布局中的所有子级之后......然后它会在第二次通过并能够正确调整滚动视图的大小。

【讨论】:

  • 非常感谢!非常适合我(我还没有完全了解相关布局)。
  • 谢谢。如果ScrollView 的内容很少(短)但确实显示正确,您的解决方案允许我将(外部)LinearLayout 不拉伸一直到其容器的底部下方的Button 而不将其压缩到1px (当您使用RelativeLayout 并让Button 低于ScrollView 时)。如果您不关心 LinearLayout 一直向下拉伸(即始终拉伸滚动区域以填充尽可能多的空间),其他一些解决方案对我来说似乎更清晰。
  • 迄今为止我见过的最佳解决方案。
  • 您应该指定android:orientation="vertical"。默认是水平的。
  • 它确实有效。你应得的,。这是一个 +1 给你的家伙。
【解决方案2】:

使用RelativeLayout。从底部的Button 开始,将ScrollView 放在Button 的上方。

Relative Layout - Android Developers

<RelativeLayout
  (...)>

    <LinearLayout android:id="@+id/ll1"
        android:layout_alignParentTop="true"
        (...)/>

    <Button android:id="@+id/button"
        android:layout_alignParentBottom="true"
        (...)/>

    <ScrollView
         android:layout_above="@id/button"
         android:layout_below="@id/ll1"
         (...)/>

</RelativeLayout>

类似的东西。写在我的脑海里,所以可能会出现一些错误。

【讨论】:

  • 谢谢。我以前试过这个,但是我在滚动视图后面有一个按钮,所以它不起作用,但它按照你写的方式工作。
  • 这将导致RelativeLayout 向下延伸到其容器的底部。这可能没问题。 请注意,RelativeLayout 的大小与其子项的位置之间不能存在循环依赖关系。例如,您不能有一个高度设置为 WRAP_CONTENT 而子级设置为 ALIGN_PARENT_BOTTOM 的 RelativeLayout。 -RelativeLayout Reference
  • 完美解决我的问题...(y)
【解决方案3】:

将按钮放置在 relativeLayout 中并将其与父底部对齐:

<RelativeLayout android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:orientation="horizontal"
    android:layout_height="50px">
<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Submit New Item"
    android:layout_below="@id/contentScroller"/>
 </RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多