Android中控件GridView实现设置行列分割线的方法示例

所属分类: 软件编程 / Android 阅读数: 37
收藏 0 赞 0 分享

前言

虽然Aandroid目前已经有RecyclerView了、非常强大的一个View、可以直接控制成ListView以及GridView等、而且画框线也比较方便、但是呢在很多情况下我们不得不仍然使用GridView来实现布局、那么在这个时候我们又要怎么来对GridViw进行画框线呢、下面将提供两种实现方式、大家可以选择一下

一、设置垂直、横向间距、通过GRIDVIEW和ITEM的背景色来实现

     1、设置GridView背景色

     2、设置水平和竖直方向间隔:android:horizontalSpacingandroid:verticalSpacing

      3、设置GridView的item的背景色及其选中后的颜色

XML代码如下

<GridView 
 android:id="@ id/gridView2" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_below="@ id/textView1" 
 android:layout_marginTop="30dp" 
 android:background="#999999" 
 android:horizontalSpacing="0.5dp" 
 android:verticalSpacing="0.5dp" 
 android:padding="2dp" 
 android:numColumns="3" > 

item布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:gravity="center" 
 android:background="@android:color/whith"> 
 
 <TextView 
  android:id="@ id/textView1" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textSize="20sp" 
  android:padding="10dp" 
  android:text="TextView" /> 
 
</LinearLayout> 

二、设置选择器

这种方式就是设置item的选择器的android:background属性来实现的

gv_selector代码

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item 
  android:state_selected="true" > 
  <shape android:shape="rectangle"> 
    <solid 
    android:color="#CCCCCC" 
    /> 
    <stroke android:width="1.0px" android:color="#999999" /> 
  </shape> 
 </item> 
 <item 
  android:state_pressed="true" > 
  <shape android:shape="rectangle"> 
    <solid 
    android:color="#CCCCCC" 
    /> 
    <stroke android:width="1.0px" android:color="#999999" /> 
  </shape> 
 </item> 
 <item> 
  <shape android:shape="rectangle"> 
   <stroke android:width="1.0px" android:color="#999999" /> 
  </shape> 
 </item> 
</selector> 

至此两种方法已经写完了、有细心的朋友可能会发现第二种方法的中间的网格线比边线要粗两倍、这也是第二种方法的一个不足的地方

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多