【问题标题】:Scroll a RelativeLayout when it contains custom Views滚动包含自定义视图的 RelativeLayout
【发布时间】:2011-06-15 18:21:14
【问题描述】:

我正在尝试制作可滚动的 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 不起作用,屏幕上没有滚动。你有什么想法我怎样才能让这个布局滚动?

【问题讨论】:

    标签: java android android-layout android-relativelayout android-scrollview


    【解决方案1】:

    您应该尝试以下 xml 文件。它适用于所有设备。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillViewport="true"
        android:padding="10px"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <RelativeLayout android:id="@+id/scrollable_places"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content">
        </RelativeLayout>
    </ScrollView>
    

    谢谢。

    【讨论】:

      【解决方案2】:

      在滚动视图中,您编写了 android:layout_height="wrap_content" 而不是使用 wrap_content 五个特定高度大小,例如 android:layout_height="100dip"

      谢谢 迪帕克

      【讨论】:

      • 非常感谢,它有效!但是我怎样才能让这个布局按宽度扩展到整个屏幕呢?例如,如果我将使用 Horizo​​ntalScrollWiew。从手机到手机宽度不同
      • 哦,我找到了。我只需要写类似 scrollView.getLayoutParams().width = context.getWindowManager().getDefaultDisplay().getWidth()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多