码迷,mamicode.com
首页 > 其他好文 > 详细

gridView,网格

时间:2014-07-31 23:18:30      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   java   os   io   

Unfortunately, after looking at the source code, I could 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.

 

另一种方式

 

GridView网格布局,默认情况下是没有网格线的

查找网上资料,找到了一种为GridView添加网格线的小技巧

 

实际上,该网格线是通过设置GridView各子项的间隔,并分别设置GridView背景色与子项背景色实现的。

 

实现方法

 

  1. 设置GridView背景色,设置水平间方向间隔属性值android:horizontalSpacing和竖直方向间隔属性值android:verticalSpacing
  2. 设置GridView子项背景色
示例代码:
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 context;  
          
        public GridViewAdapter(Context 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); //设置背景颜色  
            return result;  
        }  
          
    }  
}  
运行截图:
bubuko.com,布布扣
以上文字摘自:http://blog.csdn.net/sfshine/article/details/8764858,http://stackoverflow.com/questions/7132030/android-gridview-draw-dividers

gridView,网格,布布扣,bubuko.com

gridView,网格

标签:android   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/jinghua1425/p/3883539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!