问题
I have a GridLayout which is used to host a number of buttons, ordered in two columns. All the buttons have fixed height and width. If one of the buttons contains too much text, the layout is messed up. I'd like for the layout to maintain the rows correctly, regardless of whether buttons have too much text or not (I'll handle the case of displaying too much text later, using auto size of text).
This is my code:
<GridLayout
android:id="@+id/cont_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="2dp"
android:columnCount="2"
>
<Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
<Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
<Button android:layout_width="120dp" android:layout_height="60dp" android:text="this is a really long text"/>
<Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
<Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
<Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
</GridLayout>
This is how it looks:
this is how the layout should look (nevermind the text)
Note: I don't want to force a single line, I'd like for text to wrap if it wants, the button does not appear to change it's height, but I don't want to have it move around.
What am I missing?
Thank you.
回答1:
You should place the button inside a linearLayout and set the sizes of your button to the linear layout. Inside the linear layout place your button as both width and height as fill parent. This way your view will not be messed up.
回答2:
you probably can do it by simply adding a maximum height to the buttons
android:maxHeight="60dp"
来源:https://stackoverflow.com/questions/23890588/long-text-on-button-messes-up-gridlayout-rows