我有一個類來顯示一個表,我希望它是水平和垂直滾動,我已經看到了一些解決方案,但我無法實現他們,我把我的觀點在HorizontalScrollView,這讓我做了水平滾動,現在我想設置HorizontalScrollView爲滾動型的孩子。像這樣的東西。垂直滾動和水平滾動的Android
ApuestasScoreCard draw;
public class ApuestasScore extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new ApuestasScoreCard(this);
draw.setBackgroundColor(Color.WHITE);
ScrollView sv = new ScrollView(this)
HorizontalScrollView hsv = new HorizontalScrollView(this);
sv.addView(hsv);
hsv.addView(draw);
setContentView(hsv);
}
然後在我的視圖類,我實現OnMeasure()方法來渲染視圖。我的問題是:這是一個解決方案,或者我想要做的是不可能的。順便說一下,當我運行該解決方案的logcat的說:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
這裏是我的視圖類
public class ApuestasScoreCard extends View {
ApuestasScore apuestas;
public ApuestasScoreCard(Context context) {
super(context);
this.apuestas = (ApuestasScore) context;
setFocusable(true);
setFocusableInTouchMode(true);
//..Not Important Code
/* (non-Javadoc)
* @see android.widget.HorizontalScrollView#onMeasure(int, int)
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec+2000);
int height = MeasureSpec.getSize(heightMeasureSpec+500);
setMeasuredDimension(width, height);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
width = w/26f;
height = h/29f;
width2 = w/6.5f;
height2 = h/5f;
getRect(selX, selY, selRect);
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
// Draw the background...FFFF00 23F607
for (int i = 0; i < 29; i++) {
if (i < 3) {
canvas.drawLine(0, i * height, getWidth(), i * height, light);// Horizontales
canvas.drawLine(0, i * height + 1, width * 2, i * height + 1,
hilite);
}
if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14 || i == 17
|| i == 20 || i == 23 || i == 26 || i == 29) {
canvas.drawLine(0, i * height, getWidth(), i * height, dark);// Horizontales
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
}
}
for (int i = 0; i < 29; i++) {
canvas.drawLine(width * 2, i * height, getWidth() + 100,
i * height, light);// Horizontales
canvas.drawLine(width * 2, i * height + 1, getWidth() + 100, i
* height + 1, hilite);
if (i > 1 && i < 12) {
canvas.drawLine(i * width, 0, i * width, getHeight(), light);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
}
}
}