按钮长文本打乱了网格布局行网格、布局、按钮、文本

2023-09-07 14:37:10 作者:£剑魂メ秒杀彡

我有一个用于承载了许多按钮,下令在两列的网格布局。 所有的按钮有固定的高度和宽度。 如果其中一个按钮包含了太多的文字,布局混乱。我想的布局,以保持正确的行,而不管按钮是否有太多的文字或没有(我会处理显示太多的文字后,用文本的自动大小的情况下)。

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).

这是我的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 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.

我在想什么?

感谢你。

推荐答案

您应该把里面的LinearLayout按钮,并设置按钮的大小,以线性布局。里面的线性布局将您的按钮,宽度和高度为填充母。这样,你的观点就不会被搞砸了。

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.