Android实现计算器布局(线性布局)

📢📢📢博主发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,感觉非常有意思,忍不住分享一下给大家。点击跳转到教程
在这里插入图片描述

一:线性布局(LinearLayout)

在这里插入图片描述

二:效果图

在这里插入图片描述

三:样式(Values下的styles.xml)

 <style name="ButtonStyle" >
        <item name="android:radius">10dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">20sp</item>
        <item name="android:color">#000</item>
        <item name="android:borderlessButtonStyle">@style/TextAppearance.AppCompat.Body1</item>
    </style>

四:布局代码

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

      <EditText
          android:paddingTop="100dp"
          android:id="@+id/disp_content"
          android:cursorVisible="false"
          android:focusableInTouchMode="true"
          android:focusable="true"
          android:gravity="right"
          android:textSize="100sp"
          android:hint="0"
          android:layout_weight="0.5"
          android:layout_width="match_parent"
          android:layout_height="0dp" />

    <LinearLayout
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <Button
                android:id="@+id/btn_c"
                style="@style/ButtonStyle"
                android:text="C"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_delete"
                style="@style/ButtonStyle"
                android:text="←"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_SeekSurplus"
                style="@style/ButtonStyle"
                android:text="%"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
            <Button
                android:id="@+id/btn_division"
                style="@style/ButtonStyle"
                android:text="÷"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <Button
                android:id="@+id/btn_7"
                style="@style/ButtonStyle"
                android:text="7"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_8"
                style="@style/ButtonStyle"
                android:text="8"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_9"
                style="@style/ButtonStyle"
                android:text="9"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
            <Button
                android:id="@+id/btn_multiplication"
                style="@style/ButtonStyle"
                android:text="×"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <Button
                android:id="@+id/btn_4"
                style="@style/ButtonStyle"
                android:text="4"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_5"
                style="@style/ButtonStyle"
                android:text="5"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_6"
                style="@style/ButtonStyle"
                android:text="6"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
            <Button
                android:id="@+id/btn_Sub"
                style="@style/ButtonStyle"
                android:text="-"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <Button
                android:id="@+id/btn_1"
                style="@style/ButtonStyle"
                android:text="1"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_2"
                style="@style/ButtonStyle"
                android:text="2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_3"
                style="@style/ButtonStyle"
                android:text="3"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
            <Button
                android:id="@+id/btn_add"
                style="@style/ButtonStyle"
                android:text="+"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <Button
                android:id="@+id/btn_0"
                style="@style/ButtonStyle"
                android:text="0"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_little"
                style="@style/ButtonStyle"
                android:text="."
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btn_equle"
                style="@style/ButtonStyle"
                android:text="="
                android:layout_weight="2"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

相关链接:
菜鸟编程网线性布局教程链接

  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘟嘟的程序员铲屎官

你的鼓励将是我最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值