English 中文(简体)
2. 动态缺陷 排泄物
原标题:Dynamic Layout setLayoutParams Not Being Set

我的例行做法是在从XML中删除的<代码>可检索内动态生成的。 我试图制定<代码>。 表Rows to be a specific width, supplement them with atomutton (ButtonElement. 但没有任何内容可证明。 列出<代码>TableRow width,看来该编码为0,这可能说明为什么没有显示。

final Drawable normalBtnBg = getResources().getDrawable(R.drawable.button_states);
TableRow tableRow;
for (int x = 0; x < xDim; x++) {
    tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableLayout.LayoutParams(400, 20));
    for (int y = 0; y < yDim; y++) {
        // create the new button
        mButtons[x][y] = new ButtonElement(this, x, y);
        mButtons[x][y].setBackgroundDrawable(normalBtnBg);
        mButtons[x][y].setLayoutParams(new LayoutParams(20, 20));

        // add the button to the TableLayout
        tableRow.addView(mButtons[x][y]);
    }

    // add the TableRow to the table
    mButtonGrid.addView(tableRow, new TableLayout.LayoutParams(400, 20));
    Log.d(LOG_TAG, "trWidth - " + tableRow.getWidth());
}

tableRow.getWidth(>总回报为0. 我已经核实了这些纽子的生成,因为如果我直接加入mButtonGrid,就会显示(当然,不是在上,而是在垂直上下)。

感谢任何帮助。

EDIT:

下面是我的答复。

最佳回答

FYI解决了这一问题。 我正在使用表Layout。 LayoutParams,但需要使用表Row。 LayoutParams to make it work(从http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout”rel=“nofollow”>here):

final Drawable normalBtnBg = getResources().getDrawable(R.drawable.button_states);
TableRow tableRow;
for (int x = 0; x < xDim; x++) {
    tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableRow.LayoutParams(400, 20));
    for (int y = 0; y < yDim; y++) {
        // create the new button
        mButtons[x][y] = new ButtonElement(this, x, y);
        mButtons[x][y].setBackgroundDrawable(normalBtnBg);
        mButtons[x][y].setLayoutParams(new TableRow.LayoutParams(20, 20));

        // add the button to the TableLayout
        tableRow.addView(mButtons[x][y]);
    }

    // add the TableRow to the table
    mButtonGrid.addView(tableRow, new TableLayout.LayoutParams(400, 20));
}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签