简体   繁体   English

如何将所有按钮排成一行?

[英]How to fit all buttons in one row?

I'm doing the hangman game but I have seriously problem with the layout.我正在做刽子手游戏,但布局存在严重问题。 What i want to do is to fit all the letters in two row, and each letter has a button where the user can click on.我想要做的是将所有字母放在两行中,每个字母都有一个用户可以点击的按钮。 I tried everything but i can't fix the buttons that do not shrink.我尝试了所有方法,但无法修复不收缩的按钮。 I would be very happy if someone tell me what's the problem.如果有人告诉我有什么问题,我会很高兴。 Thank you!谢谢!

here the activity_main.xml这里是activity_main.xml

   <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        tools:layout_editor_absoluteX="-16dp"
        tools:layout_editor_absoluteY="31dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/hm1" />

        <TextView
            android:id="@+id/textView1"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="#######" />

        <TextView
            android:id="@+id/textView2"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TableLayout
            android:id="@+id/table"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

                <Button
                    android:id="@+id/A"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="A"></Button>

                <Button
                    android:id="@+id/B"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="B"></Button>

                <Button
                    android:id="@+id/C"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="C"></Button>

                <Button
                    android:id="@+id/D"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="D"
                    app:iconPadding="6dp"></Button>

                <Button
                    android:id="@+id/E"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="E"></Button>

                <Button
                    android:id="@+id/F"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="F"></Button>

                <Button
                    android:id="@+id/G"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="G"></Button>

                <Button
                    android:id="@+id/H"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="I"></Button>

                <Button
                    android:id="@+id/J"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="J"></Button>

                <Button
                    android:id="@+id/K"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="K"></Button>

                <Button
                    android:id="@+id/L"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="L"></Button>

                <Button
                    android:id="@+id/M"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="M"></Button>
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <Button
                    android:id="@+id/N"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="N"></Button>

                <Button
                    android:id="@+id/O"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="O"></Button>

                <Button
                    android:id="@+id/P"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="P"></Button>

                <Button
                    android:id="@+id/Q"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="Q"></Button>

                <Button
                    android:id="@+id/R"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="R"></Button>

                <Button
                    android:id="@+id/S"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="S"></Button>

                <Button
                    android:id="@+id/T"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="T"></Button>

                <Button
                    android:id="@+id/U"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="U"></Button>

                <Button
                    android:id="@+id/V"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="V"></Button>

                <Button
                    android:id="@+id/W"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="W"></Button>

                <Button
                    android:id="@+id/X"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="X"></Button>

                <Button
                    android:id="@+id/Y"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="Y"></Button>

                <Button
                    android:id="@+id/Z"
                    style="@style/StilePulsanti"
                    android:onClick="premuto"
                    android:text="Z"></Button>


            </TableRow>
        </TableLayout>

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

and here the style that I used for each button:(Stilepulsanti)这里是我用于每个按钮的样式:(Stilepulsanti)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="StilePulsanti">
        <item name="android:gravity">center_vertical|center_horizontal</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">15dp</item>
        <item name="android:background">#54AEDB</item>
        <item name="android:clickable">true</item>
    </style>
</resources>

If you want your buttons to shrink, add <item name="android:layout_weight">1</item> to your StilePulsanti style.如果您希望按钮缩小,请将<item name="android:layout_weight">1</item>添加到您的 StilePulsanti 样式中。

Your letters may not be visible after adding this, because you used too many buttons in one row.添加后您的字母可能不可见,因为您在一行中使用了太多按钮。 I suggest you add more rows to avoid this.我建议您添加更多行以避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM