我正在尝试制作可滚动的RelativeLayout,其中包含一些自定义视图。这是电影院的平面图,我有地方的 x,y 坐标以及它的宽度和高度(实际上只是矩形)。我只是把它放到这个RelativeLayout中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="10px">
<ScrollView
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="@+id/scrollable_places"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
我这样说:
RelativeLayout layout = (RelativeLayout) context.findViewById(R.id.scrollable_places);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(place.getWidth(),
place.getHeight());
params.leftMargin = place.getX();
params.topMargin = place.getY();
layout.addView(new Seat(context, place), params);
座位类别如下所示:
public class Seat extends View {
private Place place;
private boolean isRed = false;
public Seat(Context context, Place place) {
super(context);
this.place = place;
setOnClickListener(new OnSeatClickListener());
}
@Override
protected void onDraw(Canvas canvas) {
if (!isRed)
canvas.drawColor(Color.GREEN);
else
canvas.drawColor(Color.RED);
}
protected void setRed() {
isRed = true;
}
private class OnSeatClickListener implements OnClickListener {
@Override
public void onClick(View view) {
((Seat) view).setRed();
view.invalidate();
}
}
}
视图绘制完美。但是我有一个视图数组,当其中一些视图离开屏幕时,scrollView 不起作用,屏幕上没有滚动。你有什么想法我怎样才能让这个布局滚动?