如何在屏幕底部开始按钮,还是列表视图结束?

时间:2014-06-15 02:20:13

标签: android layout android-listview

因此,我一直在测试各种方法,使按钮显示为我想要的并且失败了。

我试图达到我为自己设定的以下要求: 答:如果列表视图中没有包含太多项目,则按钮必须位于屏幕底部(如果这有助于回答,则称为已知屏幕大小) B.列表视图必须在屏幕上方按下按钮(这部分我已经通过将按钮作为页脚添加到列表视图来解决)

我的问题是解决第一部分。通过将其添加为页脚,当列表包含少量项目时,它会显示在屏幕中间。因此,我需要帮助找到一种方法,使其保持在底部,直到列表视图变得足够大,然后通过列表视图进一步推进。

到目前为止,这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <Button
          android:id="@+id/Remove"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Remove product(s)" />

</RelativeLayout>

另外,这就是我在列表视图末尾添加按钮的方式:

LayoutInflater inflater = LayoutInflater.from(this);
  View v = inflater.inflate(R.layout.shopping_cart_activity, null);
  Button removeprod = (Button) v.findViewById(R.id.Remove);
  getListView().addFooterView(v);

我在stackoverflow上查找了各种答案,例如this one。不幸的是,没有一个答案具体到足以为我解决,也许一个例子会更有用(新的)。我还尝试添加以下按钮:

android:layout_alignParentBottom="true"

它并没有奏效。我也尝试在listadapter的布局中使用按钮,让我们说它在x个按钮中结果,其中x =列表的大小。

非常感谢任何想法,提前感谢。如果您希望我在帖子中添加更多内容,请告诉我,我会尽快修改。

3 个答案:

答案 0 :(得分:3)

我这样做的一种方法是使用LinearLayout,并添加View layout_weight="1",占据屏幕的其余部分,使按钮位于底部。当ListView变得足够大时,View仍会有layout_weight="1",但其高度为0dip。以下是我使用的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>


    <View
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:layout_weight="1"/>


    <Button
          android:id="@+id/Remove"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Remove product(s)" />

</LinearLayout>

答案 1 :(得分:0)

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


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <Button
          android:id="@+id/Remove"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerInParent="true"
          android:text="Remove product(s)" />

</RelativeLayout>

这里的诀窍是使用android:layout_alignParentBottom = "true"。试试这个,让我知道。并且无需为RelativelLayout指定orientation。它是LinearLayout的属性..

答案 2 :(得分:0)

使用下面我正在做的事情。你已经设置了关于页脚布局的列表视图,否则你设置高度和放大器时列表中的最后一项是不可见的如果你硬化页脚视图的高度,listview到fill_parent的宽度会更好。

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


          <ListView
                android:id="@android:id/_list"
                android:above="@+id/footer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>

          <Button 
                 android:id="@+id/footer"
                 android:layout_width="wrap_content"
                 android:layout_height="55dp"
                 android:layout_alignParentBottom="true"
                 android:layout_centerInParent="true"
                 android:text="Remove product(s)" />

 </RelativeLayout>