简体   繁体   中英

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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