android - 如何在 ScrollView 下制作静态按钮?

标签 android

我有以下 XML。我正在尝试在我的 ScrollView 下方制作一个静态按钮。我尝试设置权重,将按钮设置在 ScrollView 下方等。有人可以给我一种方法,让我可以让按钮保持在底部并且 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>

最佳答案

我知道这已经很晚了,但有更好的解决方案。 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 并且布局权重会导致它推迟调整 View 的大小,直到它处理完布局中的所有子项之后......然后它会在第二次通过并能够正确调整 ScrollView 的大小。

关于android - 如何在 ScrollView 下制作静态按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8271406/

相关文章:

android - 单击项目时如何更新 recyclerviews 其他项目?

java - 避免通过不受信任的证书连接

android - 如何使用实验性 Gradle 包含头文件?

android - Android发送短信的 Intent 是什么?

c# - 如何使用 lambda linq 从数据库中删除一行?

java - 为什么在我删除了 Log.foo(outputs) 后 LogCat 仍在报告?

java - 杀死应用程序 Picasso 在 android 中的应用程序中随处重新加载图像

java - Facebook 登录按钮适用于我的帐户,但不适用于我 friend 的帐户

Android SHOUTcast 请求

java - setOnClickListener() 的空指针异常