button is squeezed when it exceeds layout

2019-07-20 04:34发布

In my code, I create buttons dinamically. When I create multiple buttons is the following problem:

enter image description here

How do I get it when it happens the button is put down?

My code:

    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);
    }
}

My XML:

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

Can anyone help me? Thanks!

3条回答
我只想做你的唯一
2楼-- · 2019-07-20 05:24

You could set all of your buttons' weights to 1, but that would cause all of your buttons to become "Squished".

Do you think a HorizontalScrollField would work? I think that may be the best solution.

Just wrap your LinearLayout in a HorizontalScrollField and add your buttons to the LinearLayout as you are now.

查看更多
We Are One
3楼-- · 2019-07-20 05:24

Fitting 3 buttons in one row seems a bit messy. You could try a different layout style, perhaps a triangular setup, or change your UI design into something more compact and change the flow of how the buttons appear.

查看更多
霸刀☆藐视天下
4楼-- · 2019-07-20 05:31

I made a LinearLayout Vertical same, thanks to everyone for the answers. :)

查看更多
登录 后发表回答