Android 分线器

Android line divider

和两个朋友一起学习 android,现在我们只关注 UI 部分。一位朋友给了我们创建一个带有虚线分隔线的显示器的任务。我们正在尝试将它添加到 Adapter 文件中,以便它可以作为回收器视图的一部分加载到我们的片段中,但不知道如何加载。

这是虚线可绘制对象传入的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/dashes"
        android:background="@drawable/dashes_line"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

这是一种为 RecyclerView 设置分线器的方法

recyclerView.addItemDecoration(DividerItemDecoration(context, VERTICAL).apply {
           setDrawable(getDrawable(context, R.drawable.separator))
    })

您可以根据需要更改可绘制对象。

更新:添加可绘制对象separator.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
  <size android:height="3dp"/>
  <stroke
      android:color="#000000"
      android:dashWidth="10px"
      android:dashGap="10px"
      android:width="1dp"/>
</shape>

您可以尝试替换 Xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:background="@drawable/dashes_line"
        android:layout_width="match_parent"
        android:layout_height="wrap_parent"
        android:text="test"/>

    <ImageView
        android:id="@+id/dashes"
        android:background="@drawable/dashes_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
</LinearLayout>