【发布时间】:2013-06-18 21:25:58
【问题描述】:
好的,我正在尝试构建一个具有水平滚动视图的活动,用户可以通过滑动查看不同的“页面”。我的思路是这些“页面”会被浏览。以下是我的想法的模型(乱来看看它是否有效)
我对此进行了如下实验:
我的内容视图设置为滚动视图。 (不确定这是否是不正确的方法)
我创建了滚动视图,并按如下方式将视图放入其中:
private void setupScrollView()
{
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
int width = (int)MeasureUtils.convertDpToPixel(dpWidth, getApplicationContext());
int height = (int)MeasureUtils.convertDpToPixel(dpHeight, getApplicationContext());
_scrollView = new HorizontalScrollView(getApplicationContext());
_scrollView.setBackgroundColor(Color.CYAN);
_scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.i("DEBUG", "Screen dp width = " + dpWidth + " screen dp height = " + dpHeight);
TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.RED);
view.setText("TEST");
view.setX(0); // Start at the left of the scrollview.
view.setWidth(width); // Size it so that it fills to the right of the scrollview.
TextView view2 = new TextView(getApplicationContext());
view2.setBackgroundColor(Color.GREEN);
view2.setText("TEST2");
view2.setX(width); // Start the second "page/view" offscreen to the right where i can scroll to it
view.setWidth(width); // Fill the screen width
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setBackgroundColor(Color.MAGENTA);
layout.addView(view);
layout.addView(view2);
_scrollView.addView(layout);
}
上面的想法是,我将看到一个占据屏幕的视图,代表一个页面。此视图的颜色应为“红色”。然后我可以水平滚动到右侧并查看代表下一页的第二个视图 (view2)。此视图的颜色应为“绿色”。这不会发生。我最终看到屏幕的 1/3 或 1/2 看起来像是 view1,线性布局几乎占据了整个屏幕(与 CYAN 与滚动视图流血)。
我是不是以错误的方式处理这个问题,和/或是否有可能按照我的方式进行这项工作?
【问题讨论】:
标签: android android-layout android-widget scrollview