2013-02-28 80 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