【问题标题】:Android line divider安卓分线器
【发布时间】:2020-08-07 12:26:29
【问题描述】:

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

这是虚线drawable传入的布局

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

【问题讨论】:

    标签: android kotlin android-recyclerview rx-kotlin data-class


    【解决方案1】:

    这是一种可以为 RecyclerView 设置行分隔符的方法

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

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

    更新:添加drawable 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>
    

    【讨论】:

    • 我在哪里添加这个
    • 你可以在你的 RecyclerView 上使用它,你可以在这个视图中为你的 RecyclerView 充气
    • 你可以在你的activity或fragment上做,它的布局有recyclerview
    • @PizzaParty123 你能详细说明一下吗?您只是想添加一些虚线作为 RecyclerView 的项目,还是您已经在 RecyclerView 中有一个项目列表并想向它们添加线分隔符?
    【解决方案2】:

    你可以试试替换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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多