2016-05-16 196 views
0

我试图做这样的列表视图: Listview I want如何设置ListView项的间距

我尝试以下方法:

  1. 在列表视图中设置这一点,但它不工作

    android:divider="@android:color/transparent" 
    android:dividerHeight="10.0sp" 
    
  2. 通过设置TextView的布局marginTop,但它也不能正常工作。

所以有人可以帮助我如何获得所需的输出。

我试过thisthis,但这些都不起作用。

这是我的自定义列表视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginTop="@dimen/activity_vertical_margin" 
> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal"> 
<View 
    android:layout_width="8dp" 
    android:layout_height="wrap_content"/> 
<ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+id/imageView" 
    android:layout_margin="5dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/textView" 
    android:layout_margin="5dp" 
    style="@style/AppTheme.Text" 
    /> 
<View 
    android:layout_width="8dp" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 
<View 
    android:layout_width="match_parent" 
    android:layout_height="8dp" 
    android:background="#fff"/> 
</LinearLayout> 

我得到这个:

+0

你能发布更多关于你的ListView的细节吗?它是否有自定义视图(xml文件)? –

+0

我已更新问题@RosárioPereiraFernandes – SAVVY

+0

请勿使用** SP **作为这些属性。它用于字体大小。为更多检查此 - http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-on-android – androidnoobdev

回答

0

好了,用这个。它应该完全按照你的想法。随意改变边距和填充。

在您的自定义的ListView的布局,使用XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#009688"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:background="@drawable/rectangle"> 

     <ImageView 
      android:id="@+id/iv_imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginLeft="5dp" 
      android:background="@drawable/ic_launcher" /> 

     <TextView 
      android:id="@+id/tv_hello" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp" 
      android:layout_toRightOf="@+id/iv_imageView1" 
      android:textColor="@android:color/black" 
      android:textSize="16sp" /> 
    </RelativeLayout> 

</RelativeLayout> 

现在,您可绘制文件夹中创建一个新的XML,称之为矩形。粘贴下面的代码。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape"> 
    <stroke android:width="2dp" android:color="#ff207d94" /> 
    <padding android:left="2dp" 
     android:top="2dp" 
     android:right="2dp" 
     android:bottom="2dp" /> 
    <corners android:radius="8dp" /> 
    <solid android:color="#ffffffff" /> 
</shape> 

半径定义了您想要的矩形的曲线,随意随意更改它。

在适配器中使用自定义ListView布局。这应该工作。

快乐编码......欢呼声!

+0

@SAVVY,upvote if这工作PLZ ....谢谢! –

0

您可以调整各个项目的ListView喜欢这个例子:

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 
     android:text="Large Text" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="8dp" /> 

</LinearLayout> 

和这里是结果,只是在父布局中添加一些边距,并放置一个空白视图以占用8dp的空间,以便下一个视图的项目距离第一个和第一个边距8dp。

enter image description here

+0

它的工作,但它不是我想要的,我试图设置视图在两边,但它不工作 – SAVVY

+0

你的意思是你把它放在textview之前和之后?结果是什么? – Omar

+0

第二个图像是输出我得到的左右视图不工作我设置了背景也只能用于最后一个视图 – SAVVY