如何在listview上添加边框?

时间:2013-08-12 09:15:27

标签: android listview border

下面是我显示列表视图的代码,但问题是当我关注listview边框将删除时看到此图像边框将显示http://imgur.com/BZVqG7w当我对焦边框将删除http://imgur.com/u6rBziB为什么在焦点时删除?我没有得到问题帮我解决问题

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



        <ListView
            android:id="@+id/listMainMenu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:listSelector="@drawable/listview_selector"
            android:dividerHeight="1dip"
            android:fadeScrollbars="true" />
    </LinearLayout>


     <!---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/listviewborder" />

   <item
     android:state_pressed="true"
   android:drawable="@drawable/listview_selector_pressed" />

        </selector>




  <------- /listviewborder.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:left="1dp" android:right="1dp"  android:top="1dp" android:bottom="1dp" 
    >  
 <shape android:shape="rectangle"> 
 <gradient
 android:startColor="#0a89f1"
android:endColor="#56768d"
 android:angle="90" />
</shape>
  </item>    
    </layer-list> 



<------------listview_selector_pressed.xml-------->






           <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item> 
  <shape android:shape="rectangle">
  <solid android:color="#000000" /> 
  </shape>
  </item>   
  <item android:left="1dp" android:right="1dp"  android:top="1dp" android:bottom="1dp" 
      >  
 <shape android:shape="rectangle"> 
 <gradient
   android:startColor="#0a89f1"
  android:endColor="#56768d"
  android:angle="90" />
   </shape>
   </item>    
  </layer-list> 






               public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;

    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.main_menu_item, null);
        holder = new ViewHolder();

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtText = (TextView) convertView.findViewById(R.id.txtText);


    holder.txtText.setText(HomeMenu.Category_name.get(position));

    return convertView;





       <----- main_menu_item.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="10dp">
        <TextView 
            android:id="@+id/txtText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/text"

            android:textSize="20sp"


            android:layout_alignParentLeft="true"
            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>

2 个答案:

答案 0 :(得分:0)

为border创建一个xml文件,然后将其用作listview的可绘制文件

border.xml

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

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

    <stroke
        android:width="2dp"
        android:color="#808080" />

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

</shape>

<ListView
      android:background="@drawable/border"
 />

答案 1 :(得分:-2)

试试这个:

<ListView
            android:id="@+id/listMainMenu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/listviewborder" <!-- HERE -->
            android:listSelector="@drawable/listview_selector"
            android:dividerHeight="1dip"
            android:fadeScrollbars="true" />

希望这有帮助。