布局设计和实现:计算器UI【TableLayout、GridLayout】

这篇具有很好参考价值的文章主要介绍了布局设计和实现:计算器UI【TableLayout、GridLayout】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、使用TableLayout实现计算器UI

1.新建一个空白项目布局

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayoutandroid 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

根据自己的需求输入其他信息

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

填写完成后,点击Finish即可

2. 设计UI界面

res/layout文件夹中的XML文件中创建UI界面。在这个XML文件中,您可以使用TableLayout来设计计算器界面。

2.1 创建layout文件夹

但是默认创建出来的项目并不会包含layout布局文件夹,因此需要我们自行创建

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

res目录内,右键单击鼠标,然后选择 “New” -> “Android Resource Directory” 选项。

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

在弹出的对话框中,将 “Directory name” 设置为 “layout”,然后点击 “OK”。

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

2.2 创建xml文件

layout目录内,右键单击鼠标,然后选择 “New” -> “Layout Resource File” 选项。

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

修改一下信息

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

如果需要直接写XML代码,可点击右上角进行Code切换
android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

2.3编写布局代码

<?xml version="1.0" encoding="utf-8"?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D8ECF3">

    <!-- 第一行,显示计算结果 -->
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="90dp">

        <TextView
            android:gravity="bottom|right"
            android:textSize="70dp"
            android:singleLine="true"
            android:layout_margin="15dp"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:id="@+id/textView"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/btn_left"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="("/>
        <Button
            android:id="@+id/btn_right"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text=")"/>

        <Button
            android:id="@+id/btn_quyu"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="%"/>

        <Button
            android:id="@+id/btn_CE"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="CE"
            />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/btn_7"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="7"
            />
        <Button
            android:id="@+id/btn_8"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="8"
            />
        <Button
            android:id="@+id/btn_9"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="9"
            />
        <Button
            android:id="@+id/btn_chu"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="÷"
            />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/btn_4"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="4"
            />
        <Button
            android:id="@+id/btn_5"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="5"
            />
        <Button
            android:id="@+id/btn_6"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="6"
            />
        <Button
            android:id="@+id/btn_cheng"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="×"
            />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/btn_1"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="1"
            />
        <Button
            android:id="@+id/btn_2"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="2"
            />
        <Button
            android:id="@+id/btn_3"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="3"
            />
        <Button
            android:id="@+id/btn_jian"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="-"
            />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/btn_0"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="0"
            />
        <Button
            android:id="@+id/btn_dian"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="."
            />
        <Button
            android:id="@+id/btn_dengyu"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="="
            />
        <Button
            android:id="@+id/btn_jia"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25sp"
            android:text="+"
            />
    </TableRow>

</TableLayout>

可以看到实现了这样的一个效果

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

  1. 第一个 TableRow:

    • android:layout_widthandroid:layout_height 属性设置为 match_parent90dp,表示这个 TableRow 的宽度会填满其父容器,并且高度为 90dp。

    • TextView 元素是 TableRow 内的子元素。它有以下属性:

      • android:gravity="bottom|right":文本在 TextView 中的对齐方式,文本在底部和右边对齐。
      • android:textSize="70dp":文本的字体大小为 70dp。
      • android:singleLine="true":文本只显示在一行内。
      • android:layout_margin="15dp":设置 TextView 的外边距为 15dp。
      • android:layout_width="match_parent"android:layout_height="120dp":TextView 的宽度会填满 TableRow,高度为 120dp。
      • android:id="@+id/textView":为 TextView 分配一个唯一的标识符。
  2. 第二个 TableRow:

    • android:layout_widthandroid:layout_height 属性设置为 match_parentmatch_parent,表示这个 TableRow 的宽度会填满其父容器,高度也会填满。

    • 这个 TableRow 包含了四个 Button 元素,每个按钮代表不同的操作。

      • 每个按钮的 android:id 属性用于标识按钮。
      • android:layout_marginLeft="10dp":设置按钮之间的左外边距为 10dp。
      • android:gravity="center":设置按钮内文本的对齐方式为居中。
      • android:layout_width="0dp"android:layout_weight="1":这些属性是使用权重来均分 TableRow 中的宽度。每个按钮的宽度都会根据权重平均分配,以使它们在 TableRow 中平均分布。
      • android:textSize="25sp":按钮内文本的字体大小为 25sp。
      • android:text:按钮上显示的文本内容。这个布局代码的作用是创建一个包含一个文本框和一组操作按钮的界面,可能用于一个简单的计算器或表达式输入界面。

3.应用布局

3.1 打开主文件

打开 Kotlin Activity 文件,通常是在 src/main/java/com/yourpackage/YourActivity.kt

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

3.2 修改内容视图

在 Activity 类的 onCreate 方法中,通过 setContentView 方法将布局文件设置为 Activity 的内容视图。假设您的布局文件名为 table.xml,在 onCreate 方法中添加以下代码:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.table)
    }
}

需要注意以下几个条件:

  1. 确保替换 "table" 为您的布局文件的实际名称(不包括文件扩展名)。这样,Android 就会将该布局文件加载到 Activity 中并显示出来。
  2. 确保您的 table.xml 文件位于 res/layout 目录中。通常,它应该是这个目录的子目录下的一个 XML 布局文件。
  3. 在您的 Kotlin 代码中,您可以通过 findViewById 方法来获取布局中的视图元素,例如,如果要获取布局中的某个按钮,可以这样做:

实现效果

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout

二、使用GridLayout实现计算器UI

其他实现方式一样,对应的布局xml如下

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="4"
    android:rowCount="6"
    android:padding="16dp"
    android:layout_gravity="center"
    android:background="#D8ECF3">


    <TextView
        android:gravity="bottom|right"
        android:textSize="70dp"
        android:singleLine="true"
        android:layout_margin="15dp"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_columnSpan="4"
        android:layout_rowSpan="2" />

    <Button
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="("
        android:textSize="24sp"
        android:layout_columnSpan="1"/>

    <Button
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text=")"
        android:textSize="24sp"
        android:layout_columnSpan="1"/>
    <Button
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="%"
        android:textSize="24sp"
        android:layout_columnSpan="1"/>

    <Button
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="CE"
        android:textSize="24sp"
        android:layout_columnSpan="1"/>

    <!-- 第三行,数字按钮 -->
    <Button
        android:layout_height="wrap_content"
        android:text="7"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="8"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="9"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="÷"
        android:gravity="center"
        android:textSize="24sp"/>

    <!-- 第四行,数字按钮 -->
    <Button
        android:layout_height="wrap_content"
        android:text="4"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="5"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="6"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="×"
        android:gravity="center"
        android:textSize="24sp"/>

    <!-- 第五行,数字按钮 -->
    <Button
        android:layout_height="wrap_content"
        android:text="1"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="2"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="3"
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="-"
        android:gravity="center"
        android:textSize="24sp"/>

    <!-- 第六行,数字按钮、小数点和等号 -->
    <Button
        android:layout_height="wrap_content"
        android:text="0"
        android:gravity="center"
        android:textSize="24sp"
        android:layout_columnSpan="1"/>
    <Button
        android:layout_height="wrap_content"
        android:text="."
        android:gravity="center"
        android:textSize="24sp"/>
    <Button
        android:layout_height="wrap_content"
        android:text="="
        android:gravity="center"
        android:textSize="24sp"
        android:layout_columnSpan="1"/>
    <Button
        android:layout_height="wrap_content"
        android:text="+"
        android:gravity="center"
        android:textSize="24sp"/>

</GridLayout>

修改成下图即可android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout
实现效果

android 使用tablelayout设计计算器界面,Android,ui,Android,布局,TableLayout,GridLayout文章来源地址https://www.toymoban.com/news/detail-766256.html

到了这里,关于布局设计和实现:计算器UI【TableLayout、GridLayout】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 学会使用Android Studio网格布局制作计算器界面

    1.1GridLayout布局使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列。 1.2可以自己设置布局中组件的排列方式 1.3可以自定义网格布局有多少行、多少列 1.4可以直接设置组件位于某行某列 1.5可以设置组件横跨几行或者几列 基于-  Empty Activity 模板

    2024年02月08日
    浏览(37)
  • 【C++手撕系列】——设计日期类实现日期计算器

        😎博客昵称:博客小梦 😊最喜欢的座右铭:全神贯注的上吧!!! 😊作者简介:一名热爱C/C++,算法等技术、喜爱运动、热爱K歌、敢于追梦的小博主! 😘博主小留言:哈喽! 😄各位CSDN的uu们,我是你的博客好友小梦,希望我的文章可以给您带来一定的帮助,话不

    2024年02月13日
    浏览(29)
  • 【UniApp】-uni-app-项目实战页面布局(苹果计算器)

    经过前面的文章介绍,基本上 UniApp 的内容就介绍完毕了 那么从本文开始,我们就开始进行一个项目的实战 这次做的项目是苹果计算器,这个项目的难度不是很大,但是也不是很简单,适合练手 打开 HBuilderX,点击左上角 文件 - 新建 - 项目 : 项目创建完毕之后,首先来分析

    2024年02月04日
    浏览(39)
  • android计算器界面布局线性布局跨2行,使用Kotlin高效地开发Android App(一,GitHub标星3.2K

    get(url).placeholder(R.drawable.shape_default_round_bg) .error(R.drawable.shape_default_round_bg) // .apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))) .transform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0)) .into(this) } /** 占位符圆形 */ fun ImageView.loadCircle(url: Drawable) {

    2024年04月11日
    浏览(33)
  • 【硬件设计的工具、计算器】

    :R-C滤波器截止频率在线计算器 ---------------------------------------------------------------------------------   ---------------------------------------------------------------------------------- LC电路频率在线计算器 -------------------------------------------------------------------------------------------------------------  https:

    2024年02月15日
    浏览(34)
  • Java课程设计——简易计算器

    1.系统简介 1.1设计背景     随着人们物质生活水平的日益提高,人们对高质量的计算提出了更高的要求。当今世界,是云计算的生活和物联网发展和使用阶段,计算性能的高低和计算的效率直接影响到了人们的生活。对此,使用Java语言开发出一个为人们生活带来便利的计算

    2024年02月08日
    浏览(34)
  • JAVA web 设计 计算器

    掌握怎样在JSP中使用request对象获取form表单提交的text(文本框)以及以下select(下拉列表)中的数据。掌握使用request对象实现重定向。 (1)编写input.jsp,该页面提供一个form表单,该form表单体中两个text文本框,用于用户输入数字,提供一个select下拉列表,该下拉列表有加减

    2024年02月08日
    浏览(33)
  • Android计算器设计实验报告

    一、实验目的:        熟悉Android稍微复杂的逻辑编程,应用网格布局设计一个简单Android计算器界面并能实现加减和清零等功能。 二、实验设备:        1.PC机        2.Windows操作系统        3.Android Studio 三、实验原理简述:        TableLayout是一种可以制作表格的布局

    2024年02月09日
    浏览(41)
  • C++多态案例-设计计算器类

    多态是面向对象的三大特性之一 多态分为两类 静态多态: 函数重载 和 运算符重载 都属于静态多态,复用函数名 动态多态: 派生类 和 虚函数 实现运行时多态 静态多态和动态多态的区别 静态多态的函数地址早绑定-----编译阶段确定函数地址 动态多态的函数地址晚绑定--

    2024年02月10日
    浏览(32)
  • 【设计模式项目】【GUI计算器】

    由于Java 课程设计已经完成前期的计算器GUI设计,以及功能实现,现在基于之前的基础,进行设计模式的应用,以完善软件设计。 如果还不了解之前GUI设计的童鞋,可以先查看【Java课程设计】【图形界面程序】【计算器】。 使用了以下设计模式: 工厂模式: 工厂模式被用于

    2024年02月11日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包