为了账号安全,请及时绑定邮箱和手机立即绑定

为什么不用TableLayout布局

为什么这个布局不用TableLayout布局呢,感觉这个布局很适合做计算器?

正在回答

4 回答

<?xml version="1.0" encoding="utf-8"?>  
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" android:layout_height="wrap_content"  
    android:textSize="42sp" android:stretchColumns="1">  
    <TableRow>  
        <EditText android:id="@+id/result" android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:background="@android:drawable/editbox_background"  
            android:layout_span="4" android:textSize="48sp" android:gravity="right|center_vertical"  
            android:cursorVisible="false" android:editable="false" android:lines="1" />  
    </TableRow>  
    <TableRow>  
        <LinearLayout android:orientation="horizontal"  
            android:layout_width="fill_parent" android:layout_height="wrap_content"  
            android:textSize="42sp" android:layout_weight="1">  
            <Button android:id="@+id/num7" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="7" android:layout_weight="1" />  
            <Button android:id="@+id/num8" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="8" android:layout_weight="1" />  
            <Button android:id="@+id/num9" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="9" android:layout_weight="1" />  
            <Button android:id="@+id/divide" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="/" android:layout_weight="1" />  
        </LinearLayout>  
    </TableRow>  
    <TableRow>  
        <LinearLayout android:orientation="horizontal"  
            android:layout_width="fill_parent" android:layout_height="wrap_content"  
            android:textSize="42sp" android:layout_weight="1">  
            <Button android:id="@+id/num4" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="4" android:layout_weight="1" />  
            <Button android:id="@+id/num5" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="5" android:layout_weight="1" />  
            <Button android:id="@+id/num6" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="6" android:layout_weight="1" />  
            <Button android:id="@+id/multiply" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="*" android:layout_weight="1" />  
        </LinearLayout>  
    </TableRow>  
    <TableRow>  
        <LinearLayout android:orientation="horizontal"  
            android:layout_width="fill_parent" android:layout_height="wrap_content"  
            android:textSize="42sp" android:layout_weight="1">  
            <Button android:id="@+id/num1" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="1" android:layout_weight="1" />  
            <Button android:id="@+id/num2" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="2" android:layout_weight="1" />  
            <Button android:id="@+id/num3" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="3" android:layout_weight="1" />  
            <Button android:id="@+id/subtract" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="-" android:layout_weight="1" />  
        </LinearLayout>  
    </TableRow>  
    <TableRow>  
        <LinearLayout android:orientation="horizontal"  
            android:layout_width="fill_parent" android:layout_height="wrap_content"  
            android:textSize="42sp" android:layout_weight="1">  
            <Button android:id="@+id/num0" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="0" android:layout_weight="1" />  
            <Button android:id="@+id/point" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="." android:layout_weight="1" />  
            <Button android:id="@+id/add" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="+" android:layout_weight="1" />  
            <Button android:id="@+id/equal" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" android:textSize="42sp"  
                android:text="=" android:layout_weight="1" />  
        </LinearLayout>  
    </TableRow>  
    <TableRow>  
        <Button android:id="@+id/clear" android:layout_width="fill_parent"  
            android:layout_height="wrap_content" android:textSize="30sp"  
            android:text="clear" android:layout_span="4" android:gravity="center_vertical|center_horizontal"/>  
    </TableRow>  
</TableLayout>  
简单的实例,可以参考一下


0 回复 有任何疑惑可以回复我~

因为实例有一个跨行的=号,而tablelayout是不支持跨行的,所以最后还是要用linearlayout去嵌套,而嵌套起来不好与前面的tablelayout对齐,所以tablelayout在这个例子里反而不太好用。。


PS:最佳答案里这个例子,跟我们要实现的不太一样

0 回复 有任何疑惑可以回复我~

在实际的开发中,这个需要结合自己项目的实际情况,一般来说,布局不会像计算器那样规律分布,使用linearlayout和RelativeLayout 要更方便一些,如果布局中有相同的属性,则可以抽取出来,写出Style来引用。

0 回复 有任何疑惑可以回复我~

做计算器可以用TableLayout布局来实现,而且挺方便的。当然,用线性布局也是可以实现相应的功能,我感觉主要是看个人的习惯吧,都可以的。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android攻城狮的第一门课(入门篇)
  • 参与学习       312506    人
  • 解答问题       4931    个

想快速掌握Android应用开发基础,选择学习这门课程就对了。

进入课程

为什么不用TableLayout布局

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号