androidstudio怎么设置android 分割线控件

PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信Android Studio -修改LogCat的颜色*美爆了*
一、 先看效果
File-&Settings 或Ctrl + Alt +S 找到 Editor -& Colors &Fonts -&
Logcat 或在上面的搜索框中输入Logcat 点中Verbose , Info, Debug等选项,然后在后面将Use Inberited attributes 去掉勾选 再将 Foreground 前的复选框选上,就可以双击后面的框框去选择颜色了 Apply&&OK
三、对应色值镜花水月blog(71)
其实跟嵌套ListView一样,重写GridView的这个方法
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE && 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
然后再添加分割线
protected void dispatchDraw(Canvas canvas){
super.dispatchDraw(canvas)
View localView1 = getChildAt(0)
int column = getWidth() / localView1.getWidth()
int childCount = getChildCount()
Paint localPaint
localPaint = new Paint()
localPaint.setStyle(Paint.Style.STROKE)
localPaint.setStrokeWidth(2)
localPaint.setColor(Color.parseColor("#EFEFEF"))
for(int i = 0
View cellView = getChildAt(i)
if((i + 1) % column == 0){
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint)
}else if((i + 1) & (childCount - (childCount % column))){
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint)
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint)
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint)
if(childCount % column != 0){
for(int j = 0
View lastView = getChildAt(childCount - 1)
canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getTop(), lastView.getRight() + lastView.getWidth()* j, lastView.getBottom(), localPaint)
完整GridView代码如下:
package com.mirrorflower.meituan.
import android.content.C
import android.graphics.C
import android.graphics.C
import android.graphics.P
import android.util.AttributeS
import android.view.V
import android.widget.GridV
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE && 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
protected void dispatchDraw(Canvas canvas){
super.dispatchDraw(canvas);
View localView1 = getChildAt(0);
int column = getWidth() / localView1.getWidth();
int childCount = getChildCount();
Paint localP
localPaint = new Paint();
localPaint.setStyle(Paint.Style.STROKE);
localPaint.setStrokeWidth(2);
localPaint.setColor(Color.parseColor("#EFEFEF"));
for(int i = 0;i & childCi++){
View cellView = getChildAt(i);
if((i + 1) % column == 0){
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
}else if((i + 1) & (childCount - (childCount % column))){
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
if(childCount % column != 0){
for(int j = 0 ;j & (column-childCount % column) ; j++){
View lastView = getChildAt(childCount - 1);
canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getTop(), lastView.getRight() + lastView.getWidth()* j, lastView.getBottom(), localPaint);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:58757次
积分:1288
积分:1288
排名:千里之外
原创:71篇
评论:24条
(1)(1)(2)(3)(12)(13)(13)(16)(11)android gridview画分割线
android gridview画分割线,如图:
1。先上图:
2.具体实现代码:
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);
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 localP//画笔
localPaint = new Paint();
localPaint.setStyle(Paint.Style.STROKE);
localPaint.setColor(getContext().getResources().getColor(R.color.line));//设置画笔的颜色
for (int i = 0; i & childC 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);
3.main里的方法:
public class Main2Activity extends Activity {
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() {
public void onClick(View v) {
final MyAdapter myAdapter = new MyAdapter();
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
myAdapter.setSelectItem(position);
gridview.setAdapter(myAdapter);
private class MyAdapter extends BaseAdapter {
public int getCount() {
public Object getItem(int position) {
public long getItemId(int position) {
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));
ll.setBackgroundColor(Main2Activity.this.getResources().getColor(R.color.white));
private int currentItem = 0;
public void setSelectItem(int position) {
currentItem =
notifyDataSetChanged();
4,&activity_main2.xml代码:
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:background=&#ffffff&
tools:context=&com.zdy.bbs_bbb.clicktoast.Main2Activity&&
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&
</com.zdy.bbs_bbb.clicktoast.linegridview</textview
gride_list_item.xml
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&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginTop=&10dp&
android:text=&全部&
/&</textview</imageview
&&&& 今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格。先上图,是你想要的效果么?如果是请继续往下看。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&& 我们都知道ListView设置分割线是非常容易的,设置ListView的分割线颜色和宽度,只需要在布局中定义android:divider和android:dividerHeight属性即可。而GridView并没有这样的属性和方法,那我们改如何来做呢?
&&&&&& 博主在做这个效果之前,也参考了其他的一些方案,比如说定义一个自定义的GridView,然后在dispatchDraw()方法中在每个item的四周加上一条分割线,这是需要靠算法来实现的,最后这种方法实现的效果并不理想,会出现有些item中没有加上分割线,很难达到我们想要的这种效果。
&&&&&& 其实实现这种效果并不难,原理就是让每个item都设置成带有分割线的背景,这样就很容易实现了。
&&&&&& 首先我们来写布局:&
&&&&android:layout_width=&match_parent&&&
&&&&android:layout_height=&match_parent&&&
&&&&android:orientation=&vertical&&&&&
&&&&&<scrollview&& </scrollview&&
&&&&&&&&android:layout_width=&fill_parent&&&
&&&&&&&&android:layout_height=&wrap_content&&&
&&&&&&&&android:fillViewport=&true&&&
&&&&&&&&android:scrollbars=&none&&&&&
&&&&&&&&<com.finddreams.alipay.mygridview&& </com.finddreams.alipay.mygridview&&
&&&&&&&&&&&&android:id=&@+id/gridview&&&
&&&&&&&&&&&&android:layout_width=&fill_parent&&&
&&&&&&&&&&&&android:layout_height=&wrap_content&&&
&&&&&&&&&&&&android:horizontalSpacing=&0.0dip&&&
&&&&&&&&&&&&android:listSelector=&@null&&&
&&&&&&&&&&&&android:numColumns=&3&&&
&&&&&&&&&&&&android:scrollbars=&none&&&
&&&&&&&&&&&&android:stretchMode=&columnWidth&&&
&&&&&&&&&&&&android:verticalSpacing=&0.0dip&&/&&&
&&&&&& 因为有时候我们的Gridview中的item可能比较多,为了放得下,一般都会用一个ScrollView来嵌套起来。这时就会出现一个常见的问题,我们在开发中经常会碰到,就是当ListView或者GridView被嵌套在ScrollView中时,发现只会显示第一行的数据,后面的数据就不会显示了。至于产生这个问题的原因,可能是因为Gridview和ListView都是可以根据子item的宽高来显示大小的,但是一旦嵌套到ScrollView中就可以上下滑动,于是就不能确定到底该画多大,所以才会产生这样的问题。
&&&&& 这个问题的解决方法在网上很多,一般百度一下就能查到,下面是GridView的解决方法:
public&class&MyGridView&extends&GridView&{&&
&&&&public&MyGridView(Context&context,&AttributeSet&attrs)&{&&
&&&&&&&&super(context,&attrs);&&
&&&&public&MyGridView(Context&context)&{&&
&&&&&&&&super(context);&&
&&&&public&MyGridView(Context&context,&AttributeSet&attrs,&int&defStyle)&{&&
&&&&&&&&super(context,&attrs,&defStyle);&&
&&&&@Override&&
&&&&public&void&onMeasure(int&widthMeasureSpec,&int&heightMeasureSpec)&{&&
&&&&&&&&int&expandSpec&=&MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE&&&&2,&&
&&&&&&&&&&&&&&&&MeasureSpec.AT_MOST);&&
&&&&&&&&super.onMeasure(widthMeasureSpec,&expandSpec);&&
&&&&& 接下来,我们就定义一个带分割线的选择器,具体代码是:
&&&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&
& & & & & & & & & &&
&&&&&&&&&&&&
&&&&&&&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&
&&&&&&& &定义一个selector,在里面设置一个形状为矩形rectangle,设置这个矩形的stroke描边属性的颜色为分割线的颜色,然后在不同的state的item中设置不同的gradient渐变属性,从而实现在单个item在被点击选中时的效果。
&&&&&&&&接着就是给我们GridView的item布局中加上背景了:
&&&&android:layout_width=&fill_parent&&&
&&&&android:layout_height=&fill_parent&&&
&&&&android:layout_margin=&0.0dip&&&
&&&&android:background=&@color/griditems_bg&&&&&
&&&&<relativelayout&& </relativelayout&&
&&&&&&&&android:layout_width=&fill_parent&&&
&&&&&&&&android:layout_height=&fill_parent&&&
&&&&&&&&android:layout_centerInParent=&true&&&
&&&&&&&&android:background=&@drawable/bg_gv&&&
&&&&&&&&android:padding=&12.0dip&&&&&
&&&&&&&&<imageview&& </imageview&&
&&&&&&&&&&&&android:id=&@+id/iv_item&&&
&&&&&&&&&&&&android:layout_width=&58.0dip&&&
&&&&&&&&&&&&android:layout_height=&58.0dip&&&
&&&&&&&&&&&&android:layout_centerHorizontal=&true&&&
&&&&&&&&&&&&android:contentDescription=&@string/app_name&&/&&&
&&&&&&&&<textview&& </textview&&
&&&&&&&&&&&&android:id=&@+id/tv_item&&&
&&&&&&&&&&&&android:layout_width=&wrap_content&&&
&&&&&&&&&&&&android:layout_height=&wrap_content&&&
&&&&&&&&&&&&android:layout_below=&@id/iv_item&&&
&&&&&&&&&&&&android:layout_centerHorizontal=&true&&&
&&&&&&&&&&&&android:layout_marginTop=&5.0dip&&&
&&&&&&&&&&&&android:maxLines=&1&&&
&&&&&&&&&&&&android:textColor=&@color/commo_text_color&&&
&&&&&&&&&&&&android:textSize=&14.0sp&&/&&&
&&&&& 到这里,就要开始写代码了,定义一个Adapter,把数据填充到GridView中,这一步我想大家都应该都很清楚,这里就不多讲了,不懂的话,可以参考下面的项目代码。}

我要回帖

更多关于 android menu 分割线 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信