2

在我的代码中,我动态地创建按钮。当我创建多个按钮时,出现以下问题:

在此处输入图像描述

当按钮被放下时我如何得到它?

我的代码:

    private void showGlossary(String ContentTab) {
    LinearLayout layout;
    LinearLayout.LayoutParams p;
        layout = (LinearLayout) findViewById(R.id.GlossaryTab1);

        p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, 
                LinearLayout.LayoutParams.FILL_PARENT
        );

        Glossary = (TextView) findViewById(R.id.glossary);

    Glossary.setText("Glossário:");
    while (ContentTab.indexOf("<gloss") != -1) {
        ContentTab = ContentTab.substring(ContentTab.indexOf("<gloss"));
        uri = ContentTab.substring(ContentTab.indexOf("<gloss") + 1, ContentTab.indexOf(">"));
        Button myButton = new Button(this);
        myButton.setText(Html.fromHtml(ContentTab.substring(ContentTab.indexOf(">") + 1, ContentTab.indexOf("</gloss>"))));
        myButton.setLayoutParams(p);
        myButton.setContentDescription(uri);
        layout.addView(myButton);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(ShowPhytoterapicActivity.this, Html.fromHtml(getGlossaryItem(view.getContentDescription().toString())), Toast.LENGTH_LONG).show();
            }
            });
        if(ContentTab.indexOf("</gloss>") != -1)
        ContentTab = ContentTab.substring(ContentTab.indexOf("</gloss>") + 9);
    }
}

我的 XML:

            <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/GlossaryTab1"
            android:orientation="horizontal"
            >
            </LinearLayout>

谁能帮我?谢谢!

4

3 回答 3

1

您可以将所有按钮的权重设置为 1,但这会导致您的所有按钮变为“压扁”。

你认为a HorizontalScrollField会起作用吗?我认为这可能是最好的解决方案。

只需将您的按钮包装LinearLayout在 a 中HorizontalScrollField并将您的按钮添加到LinearLayout您现在的状态即可。

于 2012-05-25T19:37:17.377 回答
0

在一行中安装 3 个按钮似乎有点乱。您可以尝试不同的布局样式,也许是三角形设置,或者将您的 UI 设计更改为更紧凑的内容并更改按钮的显示方式。

于 2012-05-25T21:15:02.893 回答
0

我也做了LinearLayout Vertical一个,谢谢大家的回答。:)

于 2012-05-29T19:53:38.547 回答