【问题标题】:Creating Views using both XML and code - elements not showing使用 XML 和代码创建视图 - 元素不显示
【发布时间】:2012-05-31 18:51:25
【问题描述】:

我正在制作一个简单的音板应用程序 - 按钮应该出现在两列中,按下按钮会播放声音。我制作了它并使用 XML 定义了所有按钮,并且效果很好,但是由于该应用程序已经附加了一个侦听器以使用循环播放声音,因此我决定仅使用代码来定义按钮。

所以代码没有错误,应用程序启动但没有出现按钮。这里有一个类似的问题:Content not showing in dynamically created android TableLayout 虽然如果我理解正确,建议的解决方案(确保我使用TableLayout.LayoutParams)已经在这里应用了。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main_table2);

    int[] sounds = { ... };

    String[] labels = { ... };

    declareButtons(sounds, labels);
}

private void declareButtons(int[] sounds, String[] labels)
{
    TableLayout tbl = (TableLayout) this.findViewById(R.id.tblMain);
    TableRow row = null;
    Button cell;

    for (int i=0; i<sounds.length; i++)
    {
        cell = new Button(this);
        cell.setText(labels[i]);
        cell.setOnClickListener(this.getStartListener(sounds[i]));

        cell.setBackgroundDrawable(getResources().getDrawable(R.drawable.soundbutton));
        cell.setTextColor(color.white);
        TableLayout.LayoutParams cellParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, (float) 1);
        cellParams.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
        cellParams.topMargin = cellParams.leftMargin;

        if (i%2==0) // left column
        {
            row = new TableRow(this);
            cell.setLayoutParams(cellParams);
            row.addView(cell);
        }
        else // right column
        {
            cellParams.rightMargin = cellParams.leftMargin;
            cell.setLayoutParams(cellParams);
            row.addView(cell);
            row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tbl.addView(row);
        }
    }

    if (sounds.length%2 > 0) // handle uneven amount of buttons
        tbl.addView(row);
}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/leonbg2"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableLayout
            android:id="@+id/tblMain"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >


        </TableLayout>
    </ScrollView>

</LinearLayout>

这是以前定义表行的方式,效果很好:

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/buttonaw"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="@drawable/soundbutton"
                android:text="A what?"
                android:textColor="@android:color/white" />

            <Button
                android:id="@+id/buttona"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="@drawable/soundbutton"
                android:text="Argh!"
                android:textColor="@android:color/white" />
        </TableRow>

【问题讨论】:

  • 我认为您总是在循环中创建相同的 Button cell,每次迭代不应该像按钮数组或其他什么不同吗?您在哪里设置应用的视图?

标签: android android-layout tablelayout


【解决方案1】:

好吧,我设法自己解决了这个问题,摆脱了TableLayout 并使用了一堆LinearLayouts(每行一个)。该代码与在 if 语句的左列部分中的 cell.setLayoutParams(cellParams); 之前添加 row.setOrientation(LinearLayout.HORIZONTAL); 或多或少相同。这个故事的寓意是;如果你认为你需要使用 TableLayout,你可能不需要。和平。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    相关资源
    最近更新 更多