在 Android 2.1 或更低版本上运行时,动态创建的内容不会显示

发布于 2024-10-17 19:48:37 字数 2143 浏览 6 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(2

‖放下 2024-10-24 19:48:37

我遇到了类似的问题,我可以向您提出一些解决方案:

  1. 重写 onDraw() 并调用 requestLayout() 来更新动态内容
  2. (更好的解决方案)使用以下内容:
myCustomTableLayout.post(new Runnable() {
        @Override
        public void run() {
            // Size of your table is known here
            myCustomTableLayout.addDynamicalContent(
                myCustomTableLayout.getWidth(), myCustomTableLayout.getHeight());
        }
    });

我没有尝试,但我相信你可以在 onSizeChanged() 中调用 post() 方法

I had similar problem and I can propose u some solutions:

  1. Override onDraw() and invoke requestLayout() there to update the dynamic content
  2. (Better solution) Use the following:
myCustomTableLayout.post(new Runnable() {
        @Override
        public void run() {
            // Size of your table is known here
            myCustomTableLayout.addDynamicalContent(
                myCustomTableLayout.getWidth(), myCustomTableLayout.getHeight());
        }
    });

I didn't try but I believe u can invoke post() method inside onSizeChanged()

花开浅夏 2024-10-24 19:48:37

我是否缺少一个原因,为什么您不按照 支持多屏幕?然后,您可以让 Android 处理选择适当的布局并正确绘制它。

编辑:

不确定为什么它在 2.1 上不能正确绘制,但是,我会对 mRowsmButtons 进行稍微不同的初始化(紧接在 super. onSizeChangedif 之前):

if ( mRows == null ) {
    mRows = new ArrayList<TableRow>();
}
if ( mButtons == null ) {
    mButtons = new ArrayList<Button>();
}

然后我会删除以下 if 及其随附的 }

 if(mRows == null && mButtons == null){

我这么说的原因是,无论出于何种原因,如果 mRowsmButtons 之一为非空,那么您的绘图代码将永远不会用当前的 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 and mButtons a little differently (immediately after super.onSizeChanged and before your if) :

if ( mRows == null ) {
    mRows = new ArrayList<TableRow>();
}
if ( mButtons == null ) {
    mButtons = new ArrayList<Button>();
}

I'd then remove the following if and it's accompanying }:

 if(mRows == null && mButtons == null){

My reason for saying this is that if, for whatever reason, one of mRows or mButtons is non-null, then your drawing code will never be called with your current if statement. Do you explicitly initialise them to null?

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