繁体   English   中英

Android:如何对齐底部的按钮和上面的listview?

[英]Android: How can you align a button at the bottom and listview above?

我想在列表视图的底部有一个按钮。

如果我使用relativeLayout / FrameLayout,它会对齐,但listView会变得非常紧张。

(在底部的按钮后面)

在此输入图像描述

的FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </FrameLayout>
</FrameLayout>

RelativeLayout的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </RelativeLayout>
</RelativeLayout>

以上两个代码只能像第一张图像一样工作。 我想要的是第二张图片。

有人可以帮忙吗?

谢谢。

FrameLayout的目的是将事物叠加在一起。 这不是你想要的。

RelativeLayout示例中,将ListView的高度和宽度设置为MATCH_PARENT这将使其占用与其父级相同的空间量,从而占用页面上的所有空间(并覆盖按钮)。

尝试类似的东西:

<LinearLayout 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical">
  <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"/>
  <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/>
</LinearLayout>

layout_weight指示如何使用额外空间。 Button不想超出它所需的空间,因此它的权重为ListView希望占用所有额外空间,因此它的权重为1。

您可以使用RelativeLayout完成类似的操作,但如果只是这两个项目,那么我认为LinearLayout更简单。

我需要在底部并排放置两个按钮。 我使用了水平线性布局,但为按钮的线性布局分配android:layout_height="0dp"android:layout_weight="0"不起作用。 为按钮的线性布局分配android:layout_height="wrap_content" 这是我的工作布局:

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1" />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <Button
            android:id="@+id/new_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New" />

        <Button
            android:id="@+id/suggest_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Suggest" />

    </LinearLayout>

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

    <ListView android:id="@+id/ListView01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1">
    </ListView>

    <FrameLayout android:id="@+id/FrameLayout01" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content">
         <Button android:id="@+id/Button01" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="button" 
              android:layout_gravity="center_horizontal">
        </Button>
    </FrameLayout>

  </LinearLayout>

这是您正在寻找的设计。 试试吧。

RelativeLayout将忽略其子女android:layout_widthandroid:layout_height属性,如果孩子有一个正确定义它们的属性leftrighttopbottom分别值。

要在右侧图像上显示结果,显示按钮上方的列表,您的布局应如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@android:id/button1"
        android:layout_alignParentTop="true"/>

    <Button
        android:id="@android:id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@android:string/ok"/>

</RelativeLayout>

关键是要定义android:layout_alignParentTop (定义top值)和android:layout_above (定义bottom值)在RecyclerView 这样, RelativeLayout将忽略android:layout_height="match_parent" ,并且RecyclerView将被放置在Button上方。

另外,请确保查看android:layout_alignWithParentIfMissing ,如果您有更复杂的布局,并且仍需要定义这些值。

我正在使用Xamarin Android,我的要求与上面的William T. Mallard完全相同,即下面有2个并排按钮的ListView。 解决方案是这个答案在Xamarin Studio中不起作用 - 当我将ListView的高度设置为“0dp”时,ListView就消失了。

我的Xamarin Android代码如下:

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

<ListView
    android:id="@+id/ListView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_above="@+id/ButtonsLinearLayout" />
<LinearLayout
    android:id="@id/ButtonsLinearLayout"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>

我将ButtonsLinearLayout对齐到屏幕的底部,并将ListView设置为高于ButtonsLinearLayout。

在你的相对布局中,listview的高度是match_parent ,这是fill_parent(对于2.1及更早版本),所以最好的解决方案是如果你想使用相对布局然后首先声明你的按钮然后你的列表视图,使列表视图位置如上面你的按钮ID,如果你想要按钮总是在底部然后使它alignParentBottom ..片段是

<RelativeLayout 
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/rl1"><Button 
 android:layout_width="MATCH_PARENT" 
 android:layout_height="WRAP_CONTENT" 
 /><ListView 
 android:layout_width="MATCH_PARENT" 
 android:layout_height="0" 
 android:layout_above="@id/listview"/></RelativeLayout>

这可以防止您的列表视图占据整个位置,并使您的按钮出现..

@jclova你可以做的另一件事是在相对布局中使用layout-below=@+id/listviewid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM