问题
In my code, I create buttons dinamically. When I create multiple buttons is the following problem:
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!
回答1:
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.
回答2:
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.
回答3:
I made a LinearLayout Vertical
same, thanks to everyone for the answers. :)
来源:https://stackoverflow.com/questions/10760011/button-is-squeezed-when-it-exceeds-layout