android上动态创建views和getWidth方法

发布于 2024-10-17 11:31:25 字数 1994 浏览 2 评论 0原文

我在布局和视图上使用 getWidth 方法时遇到问题,因此我尝试将视图的创建和放置在父级中分开。在开始时,活动启动方法 loadResources(),该方法从数据库获取所有图标,为它们创建图像视图,并在将它们分配给relativeLayout父级后在其他循环中尝试将它们放置在屏幕上。但屏幕上什么也没有??

这是代码:

private void loadResources() {

    cursor = managedQuery(Browser.BOOKMARKS_URI, projection, selection,
    null, Browser.BookmarkColumns.VISITS + " DESC");

    if(cursor.moveToFirst()) {
        bookmarkCounter = 1;
        ByteArrayInputStream blobImage;

        do{
           bookmark = new ImageView(this);
           bookmark.setId(bookmarkCounter++);
           bookmark.setBackgroundColor(Color.WHITE);

           blobImage = new ByteArrayInputStream(
                   cursor.getBlob(cursor.getColumnIndex(BookmarkColumns.FAVICON)));

           bookmark.setImageDrawable(
                   Drawable.createFromStream(blobImage, "" + bookmark.getId()));

           params = new RelativeLayout.LayoutParams(
                   (int) (40 * scale), (int) (40 * scale));

           params.topMargin = (int) (scale * 20);
           params.leftMargin = (int) (scale * 20);
           relativeLayout.addView(bookmark, params);
        } while (cursor.moveToNext());

        int idOfViewToTheLeft = 1;

        for(int counter = 1; counter < bookmarkCounter; counter++) {

            bookmark = (ImageView) findViewById(counter);

            Log.v("WIDTH", "" + relativeLayout.getWidth());

            if(bookmarkCounter > 1) {
               if(relativeLayout.getWidth() > (bookmark.getLeft() + bookmark.getWidth())) {
                   params.addRule(RelativeLayout.RIGHT_OF, bookmark.getId() - 1);
               } else {
                   params.addRule(RelativeLayout.BELOW, idOfViewToTheLeft);
                   idOfViewToTheLeft = bookmark.getId();
               }
            }

            bookmark.setScaleType(ScaleType.CENTER_CROP);

            bookmark.setLayoutParams(params);
        }

        setContentView(R.layout.main);

     }
    }

}

I had problems with getWidth methods on layouts and views so I tried separating creation and placing of views inside parent. At start, activity starts the method loadResources() which gets all favicons from database, creates imageviews for them and in other loop after assigning them to relativeLayout parent tries placing them on screen. But nothing is on screen??

Here's the code:

private void loadResources() {

    cursor = managedQuery(Browser.BOOKMARKS_URI, projection, selection,
    null, Browser.BookmarkColumns.VISITS + " DESC");

    if(cursor.moveToFirst()) {
        bookmarkCounter = 1;
        ByteArrayInputStream blobImage;

        do{
           bookmark = new ImageView(this);
           bookmark.setId(bookmarkCounter++);
           bookmark.setBackgroundColor(Color.WHITE);

           blobImage = new ByteArrayInputStream(
                   cursor.getBlob(cursor.getColumnIndex(BookmarkColumns.FAVICON)));

           bookmark.setImageDrawable(
                   Drawable.createFromStream(blobImage, "" + bookmark.getId()));

           params = new RelativeLayout.LayoutParams(
                   (int) (40 * scale), (int) (40 * scale));

           params.topMargin = (int) (scale * 20);
           params.leftMargin = (int) (scale * 20);
           relativeLayout.addView(bookmark, params);
        } while (cursor.moveToNext());

        int idOfViewToTheLeft = 1;

        for(int counter = 1; counter < bookmarkCounter; counter++) {

            bookmark = (ImageView) findViewById(counter);

            Log.v("WIDTH", "" + relativeLayout.getWidth());

            if(bookmarkCounter > 1) {
               if(relativeLayout.getWidth() > (bookmark.getLeft() + bookmark.getWidth())) {
                   params.addRule(RelativeLayout.RIGHT_OF, bookmark.getId() - 1);
               } else {
                   params.addRule(RelativeLayout.BELOW, idOfViewToTheLeft);
                   idOfViewToTheLeft = bookmark.getId();
               }
            }

            bookmark.setScaleType(ScaleType.CENTER_CROP);

            bookmark.setLayoutParams(params);
        }

        setContentView(R.layout.main);

     }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各空 2024-10-24 11:31:25

我通过使用屏幕宽度手动计算数学并设置布局参数的边距解决了这个问题。

I solved this problem by working out the math manually with screenwidth and setting the margins of layout params.

荒岛晴空 2024-10-24 11:31:25

如果我能正确理解你的问题,我想这就是你需要看的......使用你视图的 onSizeChange()......这是一个扩展的代码片段http://syedrakibalhasan.blogspot.com /2011/02/how-to-get-width-and-height-dimensions.html

if i can understand your question correctly, i guess this is what you need to look at........... use onSizeChange() of your view........ here is an EXTENDED code snippet http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文