android - 在使用适配器和可绘制对象时尝试在 ListView 中分隔项目。 margin 不起作用

标签 android android-arrayadapter margins android-drawable

我正在尝试创建一个 ListView ,其中的项目具有圆角并且项目彼此间隔开。这是使用自定义 ArrayAdapter 完成的。

MyCustomAdapter arrAdapter = new MyCustomAdapter();
setListAdapter(arrAdapter);

我正在使用可绘制的图像来圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

  <corners android:radius="10dp" />

  <gradient
    android:angle="90"
    android:endColor="#993333"
    android:startColor="#ffffff"
    android:type="sweep" />

  <solid android:color="#ffffff" />

  <stroke
    android:width="1dp"
    android:color="#000000"  />
</shape>

并在布局中将其设置为最顶层容器的背景 - LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="@drawable/square"
android:orientation="horizontal" >

<TextView
    android:id="@+id/routeTag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/circle"
    android:padding="10dp"
    android:text="text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="some text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/stopD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:padding="5dp"
            android:text="(text)"
            android:textColor="#777777"
            android:textSize="10sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/nextTT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="9dp"
            android:paddingTop="5dp"
            android:text="text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/moreTT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/nextTrainTime"
            android:paddingBottom="5dp"
            android:text="text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="12sp" />
    </RelativeLayout>
</LinearLayout>

</LinearLayout>

出于某种原因,在图形显示中我能够完全按照我的意愿看到单个元素 - 四周都有边距(我还希望它从屏幕的左右边缘移开)。 但是当我运行它并让适配器完成它的工作时,我得到了一个项目列表,触摸屏幕的两侧并且它们之间没有空间分开形成单行,用于分隔普通列表中的项目。

任何人都知道如何让它表现出来?!我进行了广泛的搜索。

谢谢你的帮助

最佳答案

您的项目和宽屏之间的空间可以通过以下方式完成:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:left="10dp"
      android:right="10dp" 
      android:top="10dp" 
      android:bottom="10dp">
      <shape android:shape="rectangle">...</shape>
</item>

在我的例子中,我在 ListView block 上使用 android:divider 和 android:dividerHeight 来分隔 ListView 的每个元素,并使用 android:left android:正确的属性。

我得到信息的帖子是 this

关于android - 在使用适配器和可绘制对象时尝试在 ListView 中分隔项目。 margin 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15829940/

相关文章:

android - 删除 OLDEST 提交的返回堆栈 fragment

android - Log.d 报告在 Android Studio 2.3 更新后未显示

Android 多个 EditText 与 OnTextChangedListener

java - 机器人 : isDirectory() function always returns false

android - 如何在android Fragment 中设置自定义ListView?

android - 如何删除android中的自定义 ListView 项?

python - 如何从 pandas 数据透视表的样式(子集)中排除总行(边距)

html - 无法从页面中删除边距

android - 单击按钮显示编辑文本框

Android 设备在运行时自行更改水平边距