button is squeezed when it exceeds layout

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

<img src="https://i.stack.imgur.com/0wTti.jpg" alt="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!

Answer1:

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.

Answer2:

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.

Answer3:

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

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:button is squeezed when it exceeds layout