Android 实现两行 自定义的GridView作者:sfshine更新于 03月07日访问(123)评论(0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 |
package com.example.testcustomgridview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
public class CustomGridView extends ViewGroup{
private int mItemHeight = 50;
private int mPadding = 20;
public CustomGridView(Context context, AttributeSet attrs , int defStyle) {
super(context, attrs, defStyle);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int itemWidth = (width - mPadding) / 2;
int childCount = getChildCount();
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
view.measure(MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mItemHeight, MeasureSpec.EXACTLY));
}
// mItemHeight = getChildAt(0).getMeasuredHeight();
int row = (childCount + 1) / 2;
int height = row * mItemHeight + mPadding * (row -1);
setMeasuredDimension(width, height);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if (i % 2 == 0) {//左边一行
int y = i / 2 * (mItemHeight + mPadding);
view.layout(0, y, view.getMeasuredWidth(), view.getMeasuredHeight() + y);
}else {//右边一行
int x = view.getMeasuredWidth() + mPadding;
int y = i / 2 * (mItemHeight + mPadding);
view.layout(x, y, view.getMeasuredWidth()+x, view.getMeasuredHeight() + y);
}
}
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 |
package com.example.testcustomgridview;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGridView customGridView = (CustomGridView) findViewById(R.id.cg1);
TextView textView = new TextView(this);
textView.setBackgroundColor(Color.GRAY);
textView.setText("1354");
TextView textView1 = new TextView(this);
textView1.setBackgroundColor(Color.GRAY);
textView1.setText("2354");
TextView textView2 = new TextView(this);
textView2.setText("3354");
textView2.setBackgroundColor(Color.GRAY);
TextView textView3 = new TextView(this);
textView3.setText("4354");
textView3.setBackgroundColor(Color.GRAY);
TextView textView4 = new TextView(this);
textView4.setText("5354");
textView4.setBackgroundColor(Color.GRAY);
TextView textView5 = new TextView(this);
textView5.setText("6354");
textView5.setBackgroundColor(Color.GRAY);
TextView textView8 = new TextView(this);
textView8.setText("6354");
textView8.setBackgroundColor(Color.GRAY);
TextView textView6 = new TextView(this);
textView6.setText("6354");
textView6.setBackgroundColor(Color.GRAY);
TextView textView7 = new TextView(this);
textView7.setText("6354");
textView7.setBackgroundColor(Color.GRAY);
ViewGroup.LayoutParams vLayoutParams = new ViewGroup.LayoutParams(150, 80);
customGridView.addView(textView,vLayoutParams);
customGridView.addView(textView1,vLayoutParams);
customGridView.addView(textView2,vLayoutParams);
customGridView.addView(textView3,vLayoutParams);
customGridView.addView(textView4,vLayoutParams);
customGridView.addView(textView5,vLayoutParams);
customGridView.addView(textView6,vLayoutParams);
customGridView.addView(textView7,vLayoutParams);
customGridView.addView(textView8,vLayoutParams);
customGridView.setBackgroundColor(Color.RED);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
|
|