如何在自定义列表视图上创建边框

时间:2013-08-22 09:20:34

标签: android android-listview

我在我的应用程序中使用自定义列表视图,我想在listview周围创建一个像这个图像的边框

enter image description here

然而,如果我添加了角落,那么屏幕看起来就像this那样在角落显示了这么多点

但我的屏幕最终看起来像这样:

enter image description here

我正在使用自定义列表视图

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/taskscrollviewid" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="#BDBDBD"
        android:orientation="vertical">

        <RelativeLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:background="@drawable/imagelogo2"
            android:orientation="horizontal">

            <ImageView android:id="@+id/test_button_image"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:paddingLeft="5dp" android:paddingTop="20dp" android:src="@drawable/back">
            </ImageView>

            <ImageView android:id="@+id/test_button_image2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_alignParentRight="true" android:paddingLeft="5dp"
                android:paddingRight="15dp" android:paddingTop="15dp" android:src="@drawable/options1" />
        </RelativeLayout>

        <TextView android:id="@+id/Elementaryschool"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnback" android:paddingBottom="10dp"
            android:paddingLeft="5dp" android:paddingTop="10dp" android:textSize="17dp"
            android:textStyle="bold">
        </TextView>

        <LinearLayout android:id="@+id/lytContent"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
            android:layout_marginTop="10dp" android:background="@drawable/border2"
            android:orientation="vertical">

            <com.schoollunchapp.ExpandableHeightListView
                android:id="@+id/listMainMenu11" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:dividerHeight="1dip"
                android:fadingEdge="none" android:focusable="false"
                android:listSelector="@drawable/listview_selector"
                android:scrollbars="none">
            </com.schoollunchapp.ExpandableHeightListView>
        </LinearLayout>

        <TextView android:id="@+id/MiddleSchool"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:paddingBottom="10dp" android:paddingLeft="5dp"
            android:paddingTop="10dp" android:textSize="17dp" android:textStyle="bold">
        </TextView>

        <LinearLayout android:id="@+id/lytContent2"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_marginTop="10dp" android:background="@drawable/border2"
            android:orientation="vertical" android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <com.schoollunchapp.ExpandableHeightListView
                android:id="@+id/listMainMenu22" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp" android:dividerHeight="1dip"
                android:fadingEdge="none" android:focusable="false"
                android:listSelector="@drawable/listview_selector"
                android:scrollbars="none">
            </com.schoollunchapp.ExpandableHeightListView>
        </LinearLayout>

        <TextView android:id="@+id/HighSchool" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_toRightOf="@+id/btnback"
            android:paddingBottom="10dp" android:paddingLeft="5dp"
            android:paddingTop="10dp" android:textSize="17dp" android:textStyle="bold">
        </TextView>

        <LinearLayout android:id="@+id/lytContent3"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_marginTop="10dp" android:background="@drawable/border2"
            android:orientation="vertical" android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <com.schoollunchapp.ExpandableHeightListView
                android:id="@+id/listMainMenu33" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp" android:dividerHeight="1dip"
                android:fadingEdge="none" android:focusable="false"
                android:listSelector="@drawable/listview_selector"
                android:scrollbars="none">
            </com.schoollunchapp.ExpandableHeightListView>
        </LinearLayout>

        <TextView android:id="@+id/AtipicalSchool"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btnback" android:paddingBottom="10dp"
            android:paddingLeft="5dp" android:paddingTop="10dp" android:textSize="17dp"
            android:textStyle="bold">
        </TextView>

        <LinearLayout android:id="@+id/lytContent4"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_marginTop="10dp" android:background="@drawable/border2"
            android:orientation="vertical" android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <com.schoollunchapp.ExpandableHeightListView
                android:id="@+id/listMainMenu44" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp" android:dividerHeight="1dip"
                android:fadingEdge="none" android:focusable="false"
                android:listSelector="@drawable/listview_selector">
            </com.schoollunchapp.ExpandableHeightListView>
        </LinearLayout>
    </LinearLayout>

</ScrollView>

摘录border2.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000" />
        </shape>
    </item>
    <item android:right="1dp" android:top="1dp" android:bottom="1dp"
        android:left="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />

        </shape>
    </item>
</layer-list> 

摘录adapterclass.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"


    android:padding="5dp">
    <ImageView android:id="@+id/imgThumb" android:layout_width="90dp"
        android:layout_height="60dp" android:layout_marginRight="10dp"
        android:scaleType="centerCrop" android:background="@color/background"
        android:layout_alignParentLeft="true" android:layout_centerVertical="true" />
    <TextView android:id="@+id/txtText" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textColor="@color/text"
        android:textStyle="bold" android:textSize="22sp"
        android:layout_toRightOf="@+id/imgThumb"
        android:layout_centerVertical="true" />
    <ImageView android:id="@+id/imgNav" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/nav_next1"
        android:layout_alignParentRight="true" android:layout_centerVertical="true" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您需要在/ drawable文件夹中创建两个形状文件

这是代码

/抽拉/ top_rounded_corner

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

    <solid android:color="#fff000" />

   <corners
      android:bottomLeftRadius="0dp"
      android:bottomRightRadius="0dp"
      android:topLeftRadius="10dp"
      android:topRightRadius="10dp" >
 </corners>

</shape>

/抽拉/ bottom_rounded_corner

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >

  <solid android:color="#fff000" />

  <corners
     android:bottomLeftRadius="10dp"
     android:bottomRightRadius="10dp"
     android:topLeftRadius="0dp"
     android:topRightRadius="0dp" >
  </corners>

</shape>

根据您的要求更改颜色,并将此drawable设置为listview的项目背景。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;
    // Initialize component of custom_showdata_student.xml.
    if (convertView == null) {
        viewHolder = new ViewHolder();
        layoutinflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutinflater.inflate(R.layout.list_row, null);

        viewHolder.txtName = (TextView) convertView.findViewById(R.id.list_row_txtname);
        convertView.setTag(viewHoder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }// Fill data of particular position.

    if (position == 0)
        convertView.setBackgroundResource(R.drawable.top_rounded_corner);
    else if (position == getCount())
        convertView.setBackgroundResource(R.drawable.bottom_rounded_corner);
  return convertView;

}