android如何在listview中设置边框

android如何在listview中设置边框,android,android-listview,Android,Android Listview,这是我的代码,工作正常,我只想在列表视图的顶部和底部添加边框。所以,我的问题是如何在列表视图的顶部和底部添加边框。注意:我只想添加边框,在哪里可以放置边框?任何帮助都将不胜感激。谢谢 <ListView android:id="@+id/listCategory" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fade

这是我的代码,工作正常,我只想在
列表视图的顶部和底部添加边框。所以,我的问题是如何在
列表视图的顶部和底部添加边框。注意:我只想添加边框,在哪里可以放置边框?任何帮助都将不胜感激。谢谢

<ListView android:id="@+id/listCategory"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:fadeScrollbars="true"
          android:fastScrollEnabled="true"
          android:listSelector="@drawable/listview_selector"
          android:dividerHeight="1dp"
          android:layout_below="@+id/gridlayout"
          android:visibility="gone"/>

listview_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/listview_selector_focussed" />
<item android:state_pressed="true" android:drawable="@drawable/listview_selector_pressed" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>

listview\u selector\u focused.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/listview_selector_focussed" />
<item android:state_pressed="true" android:drawable="@drawable/listview_selector_pressed" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>

listview\u selector\u pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/listview_selector_focussed" />
<item android:state_pressed="true" android:drawable="@drawable/listview_selector_pressed" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#0a89f1" android:endColor="#56768d" android:angle="90" />
</shape>

添加页眉和页脚,它们将用作边框

  • 在您的活动中,您将声明您的
    列表视图

    View v=new View(this);
    v.setLayoutParams(new LayoutParams(<width>,>height>));
    v.setBackgroundColor(<your color>);
    ListView list=(ListView)findViewById(R.id.listCategory);
    list.addHeader(v);
    list.addFooter(v);
    
    View v=新视图(该视图);
    v、 setLayoutParams(新的LayoutParams(,>高度>);
    v、 setBackgroundColor();
    ListView列表=(ListView)findViewById(R.id.listCategory);
    列表.添加标题(v);
    列表.添加页脚(v);
    
  • 在父布局元素的布局文件中

    <YourParentLayoutElement
    ...
    android:paddingTop="<your_value>"
    android:paddingBottom="<your_value>"
    >
    
    
    

  • 是否使用自定义列表视图?对列表视图使用页眉页脚视图