【问题标题】:Android: Hide child dividers in ExpandableListViewAndroid:在 ExpandableListView 中隐藏子分隔符
【发布时间】:2011-03-15 18:51:41
【问题描述】:

我需要从 ExpandableListView 中完全移除分隔线。至于父项,它是一个 setDividerHeight 方法,我可以在其中传递零值。但是对于子分隔器没有类似的方法。有什么办法可以隐藏吗?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    如果您想从ExpandableListView 中完全删除分隔符,setDividerHeight 对于父项和子项都可以。子分隔线将使用与普通分隔线相同的高度绘制或由setDividerHeight() 设置。

    我正在使用一种解决方法来隐藏一个并取消隐藏另一个,只需将分隔线和项目设置为相同的颜色,如下所示:

     ExpandableListView expView = getExpandableListView();
     expView.setGroupIndicator(null);
     expView.setChildIndicator(null);
     expView.setChildDivider(getResources().getDrawable(R.color.greywhite));        
     expView.setDivider(getResources().getDrawable(R.color.white));
     expView.setDividerHeight(2);
    

    setDividerHeight 必须低于setChildDividersetDivider,否则高度将为0。

    等待更多答案...

    【讨论】:

    • 当然,如果填充的间距很烦人,您可以将 DividerHeight 设置为 0,并在自定义适配器中使用的单元格中包含一个分隔符。为了防止底部项目有分隔符,您可以隐藏分隔符 if index == getGroupCount()-1 && index2 == getChildCount()-1
    【解决方案2】:

    要隐藏子分隔线,请将其颜色设置为透明 #00000000

    在你的 color.xml 文件中定义透明

    <color name="transparent">#00000000</color>
    

    然后设置子分隔符

    listView.setChildDivider(getResources().getDrawable(R.color.transparent))
    

    或在布局xml文件中

    <ExpandableListView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:childDivider="#00000000"/>
    

    【讨论】:

    • 将分隔线设置为透明颜色并不能真正删除它。
    • 你也可以使用android:childDivider="@android:color/transparent"
    • 这仅适用于列表背后的背景与列表元素颜色相同的情况。如果你有背景颜色和元素白色,这将不起作用。
    【解决方案3】:

    你可以使用属性

    <ExpandableListView 
        android:id="@+id/interestlvExp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:divider="@null">
    </ExpandableListView>
    

    但这也会删除组列表分隔符。

    【讨论】:

      【解决方案4】:

      不要编写冗长的代码,而是使用属性ExpandableListView。 它会起作用的:-

      android:childDivider="@android:color/transparent" 
      

      【讨论】:

      • 这将隐藏分隔线,而不是删除它(它仍然会占用空间)。
      【解决方案5】:

      如果您只想删除子分隔线,您可以创建一个与子背景颜色相同颜色的可绘制对象。然后将其设置为您的子分隔符。

      ShapeDrawable sd1 = new ShapeDrawable(new RectShape()); sd1.getPaint().setColor(YOUR CHILD ITEM BACKGROUND COLOR); mExpListView.setChildDivider(sd1);

      【讨论】:

        【解决方案6】:

        将 expandableListView 的分隔高度设置为 0

        //setDividerHeight(0)
        

        然后在你的 headerView xml 底部添加一个视图

        <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_gravity="bottom"
                android:background="@android:color/darker_gray"
                />
        

        【讨论】:

          【解决方案7】:

          android:divider="@color/tranceParent"

           <ExpandableListView
                  android:id="@+id/expandableList"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@color/tranceParent"
                  app:layout_constraintTop_toTopOf="parent"
                  android:divider="@color/tranceParent"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintStart_toStartOf="parent"
                  tools:listitem="@layout/item_parent_case"/>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-30
            • 2012-09-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多