android上动态创建views和getWidth方法
我在布局和视图上使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过使用屏幕宽度手动计算数学并设置布局参数的边距解决了这个问题。
I solved this problem by working out the math manually with screenwidth and setting the margins of layout params.
如果我能正确理解你的问题,我想这就是你需要看的......使用你视图的 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