android - 如何使用 "Buttons"将 "table row"放入框架中?

标签 android button tablerow android-tablelayout

我正在制作一个计算器,但在框架内,它不合适。

我在其中使用“表格布局”。

但它显示的是这个。

enter image description here

虽然我想做成这样。

enter image description here

请帮忙,如何在框架内正确修复我的按钮?谢谢。

这是我的“按钮”的源代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#cccccc" 
        android:padding="15dp"/>

    <TableLayout
        android:id="@+id/tableID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <TableRow>
             <Button
                android:id="@+id/btn7"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="7"/>

             <Button
                android:id="@+id/btn8"
                android:text="8"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

             <Button
                 android:id="@+id/btn9"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="9"
                 android:layout_weight="1"/>

             <Button
                 android:id="@+id/btnDivide"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="÷"
                 android:layout_weight="1"/>     
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/btn4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="4"
                android:layout_weight="1"/>

            <Button
                android:id="@+id/btn5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="5"
                android:weight="1"/>

            <Button
                android:id="@+id/btn6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="6"
                android:weight="1"/>

            <Button
                android:id="@+id/btn7"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="7"
                android:weight="1"/>

       </TableRow>
    </TableLayout>
</LinearLayout>

最佳答案

在最后 3 个按钮的 xml 中,您编写 android:weight="1" 这将是 android:layout_weight="1"

    <TableRow>

        <Button
            android:id="@+id/btn4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/btn5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="5"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/btn6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="6"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/btn7"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="7"
            android:layout_weight="1"/>

   </TableRow>

关于android - 如何使用 "Buttons"将 "table row"放入框架中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21724381/

相关文章:

android - 无法在 TableRow 中动态居中 TextView

android - 在 onCreateView 中向 listFragment 添加静态 header

windows - 为什么在写入以点开头的文件时,使用 Android SDK 会出现 Windows 文件权限错误(IO 错误 13)?

html - 保留或禁用 <a href> CSS 大纲的原因

iOS:为什么我不能以编程方式为按钮设置框架?

android - 如何从 xml 文件动态添加 View ,我想在 RelativeLayout 中添加 TableRow

java - 这个 Android 代码 fragment 在做什么?

Android:在显示之前对Cursor中的SQL返回值进行算术

android - 设置边距——期望像素,获得 dps?

android - 每次点击时随机将 Image 设置为 ImageButton