简体   繁体   English

Android分线器

[英]Android line divider

Learning android with a two friends and right now we're focusing on just UI portion.和两个朋友一起学习 android,现在我们只关注 UI 部分。 One friend gave us the task of creating a display with dashed line divider.一位朋友给了我们创建一个带有虚线分隔线的显示器的任务。 We're trying to add it in the Adapter file so it can be loaded in our fragment as part of the recycler view but can't figure out how.我们试图将它添加到 Adapter 文件中,以便它可以作为回收器视图的一部分加载到我们的片段中,但不知道如何加载。

Here's the layout in where the dotted line drawable gets passed in这是虚线可绘制对象传入的布局

<?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>

this is a way you can set line divider for RecyclerView这是一种可以为 RecyclerView 设置分线器的方法

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

You can change the drawable as you see fit.您可以根据需要更改可绘制对象。

Update : add drawable separator.xml更新:添加可绘制分隔符。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>

you can try replaceing Xml file您可以尝试替换 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM