android gridview画分割线,如图:
来源:互联网 发布:淘宝评价多长时间消失 编辑:程序博客网 时间:2024/06/05 20:15
1。先上图:
2.具体实现代码:
3.main里的方法:public class LineGridView extends GridView { public LineGridView(Context context) { super(context); // TODO Auto-generated constructor stub } public LineGridView(Context context, AttributeSet attrs) { super(context, attrs); } public LineGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); View localView1 = getChildAt(0); int column = getWidth() / localView1.getWidth();//计算出一共有多少列,假设有3列 int childCount = getChildCount();//子view的总数 System.out.println("子view的总数childCount==" + childCount); Paint localPaint;//画笔 localPaint = new Paint(); localPaint.setStyle(Paint.Style.STROKE); localPaint.setColor(getContext().getResources().getColor(R.color.line));//设置画笔的颜色 for (int i = 0; i < childCount; i++) {//遍历子view View cellView = getChildAt(i);//获取子view if (i < 3) {//第一行 canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getRight(), cellView.getTop(), localPaint); } if (i % column == 0) {//第一列 canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getLeft(), cellView.getBottom(), localPaint); } if ((i + 1) % column == 0) {//第三列 //画子view底部横线 canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint); canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint); } else if ((i + 1) > (childCount - (childCount % column))) {//如果view是最后一行 //画子view的右边竖线 canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint); canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint); } else {//如果view不是最后一行 //画子view的右边竖线 canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint); //画子view的底部横线 canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint); } } }}
4, activity_main2.xml代码:public class Main2Activity extends Activity { LineGridView gridview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); gridview = (LineGridView) findViewById(R.id.gridview); findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); final MyAdapter myAdapter = new MyAdapter(); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { myAdapter.setSelectItem(position); } }); gridview.setAdapter(myAdapter); } private class MyAdapter extends BaseAdapter { @Override public int getCount() { return 5; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = View.inflate(Main2Activity.this, R.layout.gride_list_item, null); LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll); if (position == currentItem) {//选中的item ll.setBackgroundColor(Main2Activity.this.getResources().getColor(R.color.item_select)); } else { ll.setBackgroundColor(Main2Activity.this.getResources().getColor(R.color.white)); } return v; } private int currentItem = 0; public void setSelectItem(int position) { currentItem = position; notifyDataSetChanged(); } }
gride_list_item.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" tools:context="com.zdy.bbs_bbb.clicktoast.Main2Activity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/hello_world"/> <com.zdy.bbs_bbb.clicktoast.LineGridView android:padding="3dp" android:layout_margin="10dp" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/tv" android:background="#ffffff" android:gravity="center" android:listSelector="@null" android:numColumns="3" android:stretchMode="columnWidth" /></RelativeLayout>
<?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:gravity="center" android:id="@+id/ll" android:background="@color/item_select" android:orientation= "vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="全部" /></LinearLayout>
0 0
- android gridview画分割线,如图:
- android gridview画分割线
- android gridview画分割线.
- android GridView添加分割线
- Android给GridView设置单元格分割线
- Android控件GridView实现分割线
- Android控件GridView实现分割线
- Android自定义带分割线的GridView
- Android 给GridView添加分割线
- android 画分割线
- GridView 分割线
- GridView实现分割线
- GridView实现分割线
- gridview分割线
- GridView分割线设置
- GridView实现分割线
- GridView添加分割线
- 分割线的GridView
- 程序打开文件意外终止的调试-基于errorno.h
- android面试总结_2013
- Google File System阅读笔记
- 文本分类
- Maven 项目自动部署项目脚本(jboss)
- android gridview画分割线,如图:
- DLL和EXE的区别
- 安卓项目实现闪屏的功能
- overflow:hidden清除浮动原理
- Android Studio多渠道打包
- .NET一个线程更新另一个线程的UI(两种实现方法及若干简化)
- poj之旅——3977
- 闲话计算机网络中的两军问题
- Androidapk美化