2012-08-08 96 views
0

如何在ListView上放置Button?像这样imageListView上的按钮

我已经试过了:

<FrameLayout 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" > 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:text="Button" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

    </ListView> 
    </LinearLayout> 


</FrameLayout> 

Button无法访问...... 无论是ListViewButton必须是可访问。

+0

BTW按钮必须非滚动 – Andrew 2012-08-08 19:08:51

回答

10

首先,您应该使用RelativeLayout; FrameLayout用于最佳地保存单个子元素。

其次,布局从上到下依次绘制 - >回到前面。因此,您需要在XML文件中将您的Button置于其下方。

最后,您应该使用align而不是gravity

<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" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Large Text" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

     </ListView> 
    </LinearLayout> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" 
     android:text="Button" /> 

</RelativeLayout> 
+0

我将此解决方案与“FloatingActionButton”组合使用,而不是普通按钮。工作顺利! – Akshay 2017-03-03 07:53:06

0

android:focusable="false"添加到您的按钮属性,看看是否有帮助。

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:text="Button" /> 
+0

没有,如果按钮没有要上,你可以使用相对布局,并把下面的列表视图按钮列表视图的顶部它不 – Andrew 2012-08-08 19:15:17

+0

然后。像上面的答案一样。 – 0gravity 2012-08-08 19:16:40

+0

您可以尝试的最后一件事是设置'focusableInTouchMode =“false”'查看编辑。 – 0gravity 2012-08-08 19:23:23

0

使用的RelativeLayout并请使用机器人:layout_below = “@ + ID/yourIdHere” 或Android:layout_above = “@ + ID/yourIdHere”

0

使用相对布局,然后你可以设置你按钮位置按gravity使用此示例添加layout_align具有不同的值,将为你做的伎俩。 .....

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:text="Button" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
</RelativeLayout> 
...... 

下面写的,你可以在你的按钮代码中使用的保证金性质。

layout_alignParentBottom 
layout_alignParentLeft 
layout_alignParentRight 
layout_alignParentTop 
layout_alignRight 
android:layout_alignTop 
+0

这是相当不错的,但按钮不是ListView ... – Andrew 2012-08-08 19:19:02

+0

我刚刚给你一个例子,你应该在你的按钮代码中添加布局边距,我正在更新一些代码,请看看。 – 2012-08-08 19:25:26

0

这里的示例代码为如何使用相对布局与列表视图,并显示像你的形象。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" > 
    </ListView> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/listView1" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

</RelativeLayout> 
0

只需复制粘贴此。测试的解决方案。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="3"> 

<LinearLayout 
    android:id="@+id/linearLayout" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
> 

    <Button 
     android:text="Status" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     style="?android:attr/buttonBarButtonStyle" 
     android:gravity="center" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:textColor="@color/black" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView10" /> 

    <Button 
     android:text="Date" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     style="?android:attr/buttonBarButtonStyle" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:textColor="@color/black" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView6" 
     /> 

    <Button 
     android:text="Subject" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:gravity="center" 
     style="?android:attr/buttonBarButtonStyle" 
     android:textColor="@color/black" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView9" 

     /> 

</LinearLayout> 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/linearLayout" 
/> 

    </RelativeLayout>