【问题标题】:button is squeezed when it exceeds layout按钮超出布局时被挤压
【发布时间】:2012-05-25 19:01:38
【问题描述】:

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

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

我的代码:

    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>

谁能帮帮我?谢谢!

【问题讨论】:

  • 给你的按钮layout_weight。让所有按钮的权重都为 1。

标签: android android-layout android-button


【解决方案1】:

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

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

只需将您的LinearLayout 包装在HorizontalScrollField 中,然后将您的按钮添加到您现在的LinearLayout

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我也发了LinearLayout Vertical,谢谢大家的回答。 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多