Gridview, grid

Source: Internet
Author: User

Unfortunately, after looking at the source code, I cocould not see any easy way to add borders other than taking the approach of adding borders to the each cell. as a reference, I will post my solution here.

List_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal"    android:background="@drawable/list_selector">    <!-- Cell contents --></LinearLayout>

List_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item         android:state_selected="true"         android:drawable="@drawable/item_border_selected"     />    <item         android:state_pressed="true"         android:drawable="@drawable/item_border_selected"     />    <item        android:drawable="@drawable/item_border"     /></selector>

Item_border.xml

<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid         android:color="@android:color/transparent"     />    <stroke         android:width="1px"         android:color="@color/list_divider"     /></shape>

Item_border_selected.xml

<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid         android:color="@color/list_select"     />    <stroke         android:width="1px"         android:color="@color/list_divider"     /></shape>

Items_view.xml

<?xml version="1.0" encoding="utf-8"?><GridView    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_marginLeft="-1px"    android:layout_marginRight="-1px"    android:listSelector="@android:color/transparent"/>

Since all lines double in size as they join their neighboring cells, I made the divider size 1px instead of 1dp so it doesn't appear too large on some screens. also, I made the grid view have negative margins to hide the lines on either side. I hope this helps someone.

 

Another method

 

The layout of the gridview mesh. By default, there are no gridlines.

Search for online materials and find a tips for adding gridlines to the gridview

 

In fact, this gridline is achieved by setting the interval between the items in the gridview and setting the background color of the items in the gridview respectively.

 

Implementation Method

 

  1. Set the background color of the gridview and the attribute value Android: horizontalspacing and vertical interval attribute value Android: verticalspacing
  2. Set the background color of the gridview subitem
Sample Code: Main. xml
<?xml version="1.0" encoding="utf-8"?>  <GridView xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/main_GridView"      android:horizontalSpacing="1dp"      android:verticalSpacing="1dp"      android:stretchMode="columnWidth"      android:numColumns="3"      android:gravity="center"      android:listSelector="@null"      android:background="#DCDCDC"      android:layout_width="fill_parent"      android:layout_height="fill_parent">    </GridView> 

Main. Java

Package dyingbleed. iteye; import android. app. activity; import android. content. context; import android. graphics. color; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. viewgroup; import android. view. viewgroup. layoutparams; import android. widget. abslistview; import android. widget. baseadapter; import android. widget. gridview; import android. widget. textview; public class main extends activity {private gridview grid; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); grid = (gridview) findviewbyid (R. id. main_gridview); grid. setadapter (New gridviewadapter (this);} private class gridviewadapter extends baseadapter {private context; Public gridviewadapter (context) {This. context = context;} int COUNT = 100; @ override public int getcount () {return count;} @ override public object getitem (INT position) {return position ;} @ override public long getitemid (INT position) {return position ;}@ override public view getview (INT position, view convertview, viewgroup parent) {textview result = new textview (context ); result. settext ("item" + position); result. settextcolor (color. black); result. settextsize (24); result. setlayoutparams (New abslistview. layoutparams (New layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); result. setgravity (gravity. center); result. setbackgroundcolor (color. white); // set the background color return result ;}}}
Run: The above text from: http://blog.csdn.net/sfshine/article/details/8764858,http://stackoverflow.com/questions/7132030/android-gridview-draw-dividers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.