在 Android 2.1 或更低版本上运行时,动态创建的内容不会显示
我是 Android 应用程序开发新手,我正在尝试创建充满小按钮的屏幕。为此,我创建了 TableLayout 类,其中我使用以下代码覆盖 onSizeChanged 方法:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if(mRows == null && mButtons == null){
Integer height = new Integer(mActivity.getString(R.string.buttonHeight));
Integer width = new Integer(mActivity.getString(R.string.buttonWidth));
mColumnCount = h / height.intValue();
mRowCount = w / width.intValue();
mRows = new ArrayList<TableRow>();
mButtons = new ArrayList<Button>();
int buttonID = 0;
for(int i = 0; i < mColumnCount ; i++) {
mRows.add(new TableRow(mActivity));
mRows.get(i).setPadding(0, 0, 0, 0);
addView(mRows.get(i));
for(int j = 0; j < mRowCount; j++){
mButtons.add(new Button(mActivity));
mButtons.get(buttonID).setLayoutParams(new TableRow.LayoutParams(width.intValue(), height.intValue()));
mButtons.get(buttonID).setBackgroundResource(R.drawable.btn_normal);
mButtons.get(buttonID).setClickable(true);
mButtons.get(buttonID).setSelected(false);
mButtons.get(buttonID).setPadding(0, 0, 0, 0);
mButtons.get(buttonID).setOnClickListener(this);
mButtons.get(buttonID).setId(buttonID);
mRows.get(i).addView(mButtons.get(buttonID));
buttonID++;
}
}
defineBorders();
}
}
现在,代码在 android 2.2 或更高版本上运行时工作正常,但在 android 2.1 或更低版本上运行时,我会得到空白屏幕。我没有任何需要 android 2.2 才能执行的组件。实际上,所有组件几乎都是 API 级别 1 的东西。
此外,如果布局是在 Activity.setContentView() 上作为参数传递之前创建的,那么代码在 2.1 上也可以正常工作。但我需要屏幕尺寸参数来使布局适应不同的屏幕尺寸,并且根据我的理解,我只能从 view.onSizeChanged 方法获取它。
另外,当我搜索调用堆栈时。当框架调用 view.onSizeChanged 时,它在 android 2.1 上显示以下序列:
onSizeChanged(), onLayout()
当android 2.2上的顺序是:
onSizeChanged(), onLayout(), onMeasure(), onLayout()
这可能是框架视图缺陷吗?还是2.1之后视图框架发生了变化?关于行动方针有什么建议吗?
I'm new to Android application development and i'm trying to create screen full of small buttons. For the purpose i have created TableLayout class where i overwrite onSizeChanged method with following code:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if(mRows == null && mButtons == null){
Integer height = new Integer(mActivity.getString(R.string.buttonHeight));
Integer width = new Integer(mActivity.getString(R.string.buttonWidth));
mColumnCount = h / height.intValue();
mRowCount = w / width.intValue();
mRows = new ArrayList<TableRow>();
mButtons = new ArrayList<Button>();
int buttonID = 0;
for(int i = 0; i < mColumnCount ; i++) {
mRows.add(new TableRow(mActivity));
mRows.get(i).setPadding(0, 0, 0, 0);
addView(mRows.get(i));
for(int j = 0; j < mRowCount; j++){
mButtons.add(new Button(mActivity));
mButtons.get(buttonID).setLayoutParams(new TableRow.LayoutParams(width.intValue(), height.intValue()));
mButtons.get(buttonID).setBackgroundResource(R.drawable.btn_normal);
mButtons.get(buttonID).setClickable(true);
mButtons.get(buttonID).setSelected(false);
mButtons.get(buttonID).setPadding(0, 0, 0, 0);
mButtons.get(buttonID).setOnClickListener(this);
mButtons.get(buttonID).setId(buttonID);
mRows.get(i).addView(mButtons.get(buttonID));
buttonID++;
}
}
defineBorders();
}
}
Now the code works fine when runnings against android 2.2 or higher but i'll get blank screen when i run it against android 2.1 or lower. I don't have any components that requires android 2.2 in order to execute. Actually all the components are pretty much API level 1 stuff.
Besides the code works fine also on 2.1, if the layout is created before it's passed as a parameter on Activity.setContentView(). But i need screen size parameter to make the layout adapt on different screen sizes and based on my understanding i can only get it from view.onSizeChanged method.
Also, when i search the call stack. it shows following sequence on android 2.1 when framework calls view.onSizeChanged:
onSizeChanged(),
onLayout()
When as the sequence on android 2.2 is:
onSizeChanged(),
onLayout(),
onMeasure(),
onLayout()
Is this possibly framework view defect? Or is the view framework changed after 2.1? Any suggestions on course of action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,我可以向您提出一些解决方案:
我没有尝试,但我相信你可以在 onSizeChanged() 中调用 post() 方法
I had similar problem and I can propose u some solutions:
I didn't try but I believe u can invoke post() method inside onSizeChanged()
我是否缺少一个原因,为什么您不按照 支持多屏幕?然后,您可以让 Android 处理选择适当的布局并正确绘制它。
编辑:
不确定为什么它在 2.1 上不能正确绘制,但是,我会对
mRows
和mButtons
进行稍微不同的初始化(紧接在super. onSizeChanged
和if
之前):然后我会删除以下
if
及其随附的}
:我这么说的原因是,无论出于何种原因,如果
mRows
或mButtons
之一为非空,那么您的绘图代码将永远不会用当前的if
来调用> 声明。您是否明确将它们初始化为 null?Am I missing a reason why you don't create your layouts in xml, for each screen size/density you expect to support as per Supporting Multiple Screens? You can then let Android handle selecting the appropriate layout and drawing it correctly.
EDIT:
Not certain why it doesn't draw properly on 2.1, however, I would do the initialisation of
mRows
andmButtons
a little differently (immediately aftersuper.onSizeChanged
and before yourif
) :I'd then remove the following
if
and it's accompanying}
:My reason for saying this is that if, for whatever reason, one of
mRows
ormButtons
is non-null, then your drawing code will never be called with your currentif
statement. Do you explicitly initialise them to null?