【问题标题】:Divider won't show on ListView分隔线不会显示在 ListView 上
【发布时间】:2014-04-25 11:55:28
【问题描述】:

帮助!我尝试了所有关于 SO 的答案,但无济于事

形状 xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <!-- stroke width must be 1px, see https://stackoverflow.com/questions/3979218/android-listview-divider -->  
    <stroke 
        android:width="1px"
        android:color="#aaAAaa" />  
</shape>

在 ListFragment 中:

public void onViewCreated(View v, Bundle bundle){
  listView = getListView();
  listView.setDivider(getActivity().getResources().getDrawable(R.drawable.xxxx));
  listView.setDividerHeight(1);
  setListAdapter(...);
}

适配器:

@Override public boolean areAllItemsEnabled(){return true;}

另外,分隔线不应该跨越整个列表视图的宽度,我想在左右两边都有一些填充。所以我认为一个可绘制的形状是必须的。

已编辑答案: 不要使用线条,而是使用矩形。 并按照这个填充: How to add padding to gradient <shape> in Android?

【问题讨论】:

  • 尝试将宽度设置为 1 dp 以进行描边。这也可能有帮助。
  • 您是否从 isEnabled 返回 true?检查this。可能会有所帮助。

标签: android android-listview android-listfragment


【解决方案1】:

在列表视图的 xml 中指定以下内容:

 <ListView
        android:id="@+id/List"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:divider="#35FFFFFF"
        android:dividerHeight="1dp" >
    </ListView>

分隔线的颜色和高度可以是任何你想要的。

【讨论】:

  • 你可以像这样使用一些东西, lv = getListView(); lv.setDivider(getActivity().getResources().getDrawable(android.R.color.black)); lv.setDividerHeight(1);
  • 请记住 setDividerHeight 在 px 中有效,而不是在 dp 中。如果您打算改用 dps,则需要进行一些转换。
【解决方案2】:

尝试使用,

listView = getListView();
listView.setDivider(new ColorDrawable(Color.parseColor("#aaAAaa")))
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,getResources().getDisplayMetrics());
listView.setDividerHeight((int)px)

P.S. 更新了代码以获取给定 int 值的 dip 值。你可以在我设置1的地方传递值。

【讨论】:

  • 请记住 setDividerHeight 在 px 中有效,而不是在 dp 中。如果您打算改用 dps,则需要进行一些转换。
【解决方案3】:

使用

 <ListView
    android:id="@+id/listview"   
    android:dividerHeight="3dp" 
    android:divider="#05806D"
    android:listSelector="#ffffff"
    android:choiceMode="singleChoice"
    android:layout_width="fill_parent"
     android:layout_height="fill_parent" >
</ListView> 

在你得到解决方案之前告诉我?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2013-06-12
    相关资源
    最近更新 更多