2013-02-28 72 views
0

我正在使用使用內聯刪除按鈕的自定義ListView,但當ListView行超過1行時無法讓按鈕垂直拉伸。在ListView中垂直拉伸按鈕

我也有一些問題,讓文本尊重按鈕的邊界,雖然這是工作更早,所以我懷疑這是一個簡單的修復。

我使用的代碼如下:

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

<TextView 
    android:id="@+id/txt_item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:gravity="center_vertical" 
    android:textStyle="bold" 
    android:textSize="22sp" 
    android:textColor="#000000" 
    android:textIsSelectable="false" 
    android:layout_margin="8dp" /> 

<Button 
    android:id="@+id/btn_delete" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/delete" 
    android:contentDescription="@string/delete"/> 

</RelativeLayout> 

它找出來,像這樣:

My ListView

+0

什麼'@繪製/ delete'?它看起來像一個圖像(帶有X的紅色矩形),不是嗎? – 2013-02-28 13:37:29

+0

是的,這是一個9patch drawable,我將按鈕的背景設置爲 – 2013-02-28 13:38:59

+0

請分享9patch。 – 2013-02-28 13:50:51

回答

1

試試這個:

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

<TextView 
    android:id="@+id/txt_item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:textStyle="bold" 
    android:textSize="22sp" 
    android:textColor="#000000" 
    android:textIsSelectable="false" 
    android:layout_margin="8dp" 
    android:layout_weight="1" 
    android:singleLine="false" 
    android:text=""/> 

<Button 
    android:id="@+id/btn_delete" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/ic_launcher" 
    android:contentDescription="@string/delete"/> 

</LinearLayout> 
+0

Cripes,我不敢相信它和使用LinearLayout而不是Relative相同。我所要做的就是將'layout_height'從你的代碼改爲'match_parent',這是很好的。非常感謝 – 2013-02-28 13:32:21

0

您需要設置按鈕也

 android:layout_centerVertical="true" 


<Button 
    android:id="@+id/btn_delete" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:background="@drawable/delete" 
    android:contentDescription="@string/delete"/> 

我認爲它可以幫助你。

1

添加此屬性按鈕

android:scaleType="fitXY" 

圖像視圖被拉伸,但圖像拿着它沒有。請更改

android:background="@drawable/delete" 

android:src="@drawable/delete" 
+0

在此之後,您將在按鈕的背景中出現另一個文本問題。 :) – 2013-02-28 13:32:40