我有一个类来显示一个表格,我希望它可以水平和垂直滚动,我已经看到了一些解决方案,但我无法实现它们,我将我的视图放在一个 HorizontalScrollView 中,这让我可以做水平滚动,现在我正在尝试将 HorizontalScrollView 设置为 ScrollView 的子项。像这样的东西。
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);
}
}
}
}