我正在嘗試使可滾動RelativeLayout包含一些自定義視圖。這是電影院的計劃,我有x,y座標的地方,它的寬度和高度(實際上只是矩形)。我只是把它變成這樣的RelativeLayout:當它包含自定義視圖時滾動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不起作用,屏幕上沒有滾動。你有什麼想法,我怎樣才能讓這個佈局滾動?
Thanx,它也可以!而且更聰明的轉向 – arbuzz 2011-06-15 11:03:13