2012-08-08 52 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>