简体   繁体   English

分频器不会显示在ListView上

[英]Divider won't show on ListView

Help! 救命! I tried all answers on SO but to no avail 我尝试了所有答案,但无济于事

Shape xml: 形状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>

In ListFragment: 在ListFragment中:

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

Adapter: 适配器:

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

Also, the divider should not span the entire listview's width, I want to have some padding on the left and right. 另外,分隔线不应覆盖整个listview的宽度,我想在左右两侧留一些填充。 So I think a shape drawable is a must. 因此,我认为必须绘制形状。

EDITED Answer: Don't use line, use a rectangle instead. 编辑答案:不要使用直线,而应使用矩形。 and follow this for padding: How to add padding to gradient <shape> in Android? 并按照以下步骤进行填充: 如何在Android中为渐变<shape>添加填充?

In xml of listview specify following : 在listview的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>

The divider color and height can be anything that you want. 分隔线的颜色和高度可以是您想要的任何颜色。

Try using, 尝试使用,

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

PS updated code to get dip value for given int value. PS更新代码以获取给定int值的dip值。 You can pass the value on place where I've set 1 . 您可以将值传递到我设置为1

Use 采用

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

let me know till you get the solution or not ? 让我知道直到您找到解决方案?

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

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