如何的Android编程方式来创建滚动视图,并添加编程方式创建的视图进去 [英] Android how to programatically create scrollview and add programatically created views into it
问题描述
好吧,我试图建立一个具有水平滚动型的活动,用户可以通过滑动来查看不同的页面。我的思路是,这些页面会的意见。下面是我的想法的小样(浪费时间,看它是否有效)
Alright I'm trying to build an activity that has a horizontal scrollview, that the user can swipe through, to view different "pages". My train of thought is these "pages" will be views. The following is a mockup of my idea (to mess around to see if it works)
我这个试验如下:
我的内容视图设置为滚动视图。 (不能确定这是否是一个不正确的方法)
My content view is set to the the scrollview. (unsure if this is an incorrect approach)
我创建滚动视图,并将视图进去如下:
I create the scrollview, and place a view into it as follows:
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);
}
上面的想法是,我会看到一个观点,即占用屏幕,再presenting的页面。该视图应为红颜色。那么我可以水平滚动到右侧,看到第二个视图(视图2)再presenting下一页。这种观点应该是彩色的绿色。这不会发生。我最终看到什么样子的1/3或我的屏幕是厂景的1/2,在LinearLayout中占用的几乎的整个屏幕(有点差距的右边缘,其中从青色滚动型出血通过)。
The idea above is that I will see a view, that takes up the screen, representing a page. This view should be "RED" in color. I can then scroll horizontally to the right and see the second view (view2) representing the next page. This view should be "GREEN" in color. This does not happen. I end up seeing what looks like 1/3rd or 1/2 of my screen being view1, the linearlayout taking up almost the whole screen (a bit of a gap to the right edge where the CYAN from the scrollview bleeds through).
我承认我是新来的Android(iOS版自推出)。
I will admit I am new to Android (coming from iOS).
我是不是接近这个错误的方式,和/或是否有可能使这项工作的方式我要去呢?
Am I approaching this the wrong way, and/or is it possible to make this work the way I'm going at it?
谢谢!
推荐答案
好吧,我已经想通了什么我做错了,它竟然是一些非常小的...
Alright I've figured out what I was doing wrong, and it turned out to be something very small...
完整code是在这里:
The complete code is here:
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);
view.setWidth(width);
view.setHeight(height - 50);
TextView view2 = new TextView(getApplicationContext());
view2.setBackgroundColor(Color.GREEN);
view2.setText("TEST2");
view2.setX(0);
view2.setWidth(width);
view2.setHeight(height - 50);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setBackgroundColor(Color.MAGENTA);
layout.addView(view);
layout.addView(view2);
_scrollView.addView(layout);
}
这将创建一个水平滚动视图编程,因为我有,但问题是,我是设置第二个观点是宽了,当它应被设置为0可略见一斑:
This creates a horizontal scrollview programmatically, as I had, but the problem was that I was setting the second view to be "width" away, when it should be set to "0"as can be seen by:
view2.setX(0);
就这样,我得到2意见类似于在我的滚动视图页面,我可以刷卡通过。每次服用了整个页面。
With that, I get 2 "views" that resemble pages in my scrollview that I can swipe through. Each taking up the whole page.
恨具有code接近,它是一个简单的解决,我错过了:|
Hate having the code close and it being a simple fix that I missed :|
希望这有助于其他人试图做这样的。我要去寻找到了PageViewer弗兰克建议。
Hope this helps anyone else that tries to do it this way. I'm going to look into the PageViewer as Frank suggested.
这篇关于如何的Android编程方式来创建滚动视图,并添加编程方式创建的视图进去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!