Android如何以编程方式创建滚动视图并将编程创建的视图添加到其中 [英] Android how to programmatically create scrollview and add programmatically created views into it

查看:56
本文介绍了Android如何以编程方式创建滚动视图并将编程创建的视图添加到其中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试构建一个具有水平滚动视图的活动,用户可以在其中滚动浏览以查看不同的页面".我的思路是这些页面"将成为视图.以下是我的想法的模型(四处看看是否可行)

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)

我对此进行了如下尝试:

I've experimented with this as follows:

我的内容视图设置为滚动视图. (不确定这是否是错误的方法)

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);
}

上面的想法是,我将看到一个视图,它占据了屏幕,并代表了一个页面.此视图的颜色应为红色".然后,我可以水平向右滚动并查看代表下一页的第二个视图(view2).此视图的颜色应为绿色".这不会发生.我最终看到的是屏幕1,看起来是屏幕的1/3或1/2,线性布局几乎占据了整个屏幕(从屏幕右上角到CYAN的右边缘有一点缝隙)滚动视图出血).

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).

我以错误的方式来处理这个问题,和/或是否有可能按照我的方式来进行这项工作?

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...

完整的代码在这里:

    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.

讨厌关闭代码,这是我错过的一个简单解决方法:|

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆