当前位置: 首页 > article >正文

Android入门

下载Android studio,创建第一个项目
在这里插入图片描述
模板可以选择empty views Activity
在这里插入图片描述
在这个界面可以修改,使用语言,项目名字,存储路径以及适用版本
在这里插入图片描述
完成后,得到一个最初始的Android 项目,红色标记的两个文件,一个是负责逻辑的java文件,一个是负责界面设计的xml文件
在这里插入图片描述

布局文件以xml为后缀,主要使用,线性布局和相对布局,虽然也可以用图形拖拽的方式设计界面,但是细调还是要理解代码
以计算器的布局为例,
首先线性布局,多用嵌套,分为垂直和水平两种方向,设计一个计算器的思路是。如下设计就可以实现,两行,每行有三列button
<线性垂直分布>
<线性垂直分布>



</线性垂直分布>
<线性垂直分布>



</线性垂直分布>
</线性垂直分布>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="154dp"
        android:orientation="vertical">


        <EditText
            android:id="@+id/editTextText"
            android:layout_width="wrap_content"
            android:layout_height="72dp"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="text"
            android:text="Name" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"

        android:layout_height="64dp"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher_foreground"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_weight="1"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="12dp"
            android:layout_weight="1"
            android:text="3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:orientation="horizontal">

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

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_weight="1"
            android:text="5" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="8dp"
            android:layout_weight="1"
            android:text="6" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal">

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

        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_weight="1"
            android:text="8" />

        <Button
            android:id="@+id/button0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="8dp"
            android:layout_weight="1"
            android:text="9" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="0" />

        <Button
            android:id="@+id/button10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_weight="1"
            android:text="+" />

        <Button
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="8dp"
            android:layout_weight="1"
            android:text="=" />
    </LinearLayout>
</LinearLayout>

如果使用相对布局,要确定谁在谁的上方或者下方

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.test.MainActivity"

    android:padding="15dp"
    android:layout_gravity="center"
    android:background="#111"
    >

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button13"
        android:layout_alignBottom="@+id/button13"
        android:layout_toLeftOf="@+id/button2"
        android:background="#a10b39"
        android:padding="10dp"
        android:text="="
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"/>

    <Button
        android:id="@+id/button9"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button10"
        android:layout_alignBottom="@+id/button10"
        android:layout_alignLeft="@+id/button7"
        android:background="#666"
        android:padding="10dp"
        android:text="7"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:layout_width="wrap_content"
        android:layout_marginBottom="3dp"
        />

    <Button
        android:id="@+id/button11"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button10"
        android:layout_alignBottom="@+id/button10"
        android:layout_toRightOf="@+id/button13"
        android:background="#666"
        android:padding="10dp"
        android:text="9"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:layout_width="wrap_content"
        android:layout_marginBottom="3dp"
        />

    <Button
        android:id="@+id/button10"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button18"
        android:layout_below="@+id/button17"
        android:background="#666"
        android:padding="10dp"
        android:text="8"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:layout_width="wrap_content"
        android:layout_marginBottom="3dp"
        />

    <Button
        android:id="@+id/button20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button14"
        android:layout_alignParentRight="true"
        android:background="#a10b39"
        android:padding="10dp"
        android:text="←"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp"/>

    <Button
        android:id="@+id/button19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button20"
        android:layout_alignBottom="@+id/button20"
        android:layout_toLeftOf="@+id/button20"
        android:background="#a10b39"
        android:padding="10dp"
        android:text="CE"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:layout_marginBottom="3dp"
        />

    <Button
        android:id="@+id/button18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button10"
        android:layout_toLeftOf="@+id/button11"
        android:background="#a10b39"
        android:padding="10dp"
        android:text="±"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:layout_marginBottom="3dp"
        />

    <Button
        android:id="@+id/button17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="5dp"
        android:background="#a10b39"
        android:padding="10dp"
        android:text="√"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:layout_marginBottom="3dp" />

    <Button
        android:id="@+id/button14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button18"
        android:background="#d89218"
        android:padding="10dp"
        android:text="÷"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button10"
        android:layout_toRightOf="@+id/button6"
        android:background="#666"
        android:padding="10dp"
        android:text="5"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp"
        android:layout_marginRight="3dp"/>

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button7"
        android:layout_alignBottom="@+id/button7"
        android:layout_alignParentLeft="true"
        android:background="#666"
        android:padding="10dp"
        android:text="4"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:layout_marginBottom="3dp" />

    <Button
        android:id="@+id/button15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button7"
        android:layout_alignBottom="@+id/button7"
        android:layout_alignParentRight="true"
        android:background="#d89218"
        android:padding="10dp"
        android:text="×"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button7"
        android:layout_alignBottom="@+id/button7"
        android:layout_toRightOf="@+id/button10"
        android:background="#666"
        android:padding="10dp"
        android:text="6"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp"
        android:layout_marginRight="3dp" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button7"
        android:layout_toRightOf="@+id/button1"
        android:background="#666"
        android:padding="10dp"
        android:text="2"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp"
        android:layout_marginRight="3dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_alignParentLeft="true"
        android:background="#666"
        android:padding="10dp"
        android:text="1"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp"
        android:layout_marginRight="3dp" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_toRightOf="@+id/button7"
        android:background="#666"
        android:padding="10dp"
        android:text="3"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp"
        android:layout_marginRight="3dp"/>

    <Button
        android:id="@+id/button16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button5"
        android:layout_alignBottom="@+id/button5"
        android:layout_alignLeft="@+id/button15"
        android:background="#d89218"
        android:padding="10dp"
        android:text="-"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginBottom="3dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button4"
        android:layout_alignBottom="@+id/button4"
        android:layout_alignParentRight="true"
        android:background="#d89218"
        android:padding="10dp"
        android:text="+"
        android:textColor="#fff"
        android:textSize="10dp" />

    <Button
        android:id="@+id/button13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toLeftOf="@+id/button4"
        android:padding="10dp"
        android:text="."
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp"
        android:background="#d89218"  />

    <Button
        android:id="@+id/button12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button13"
        android:layout_alignBottom="@+id/button13"
        android:layout_alignParentLeft="true"
        android:background="#666"
        android:padding="10dp"
        android:text="0"
        android:textColor="#fff"
        android:textSize="10dp"
        android:layout_marginRight="3dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/editText1"
        android:background="#666"
        android:text=" "
        android:textColor="#fff"
        android:textSize="15dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/button17"
        android:layout_below="@+id/textView1"
        android:background="#666"
        android:ems="10"
        android:singleLine="true"
        android:textColor="#000"
        android:textSize="28dp" />

</RelativeLayout>

http://www.kler.cn/news/321312.html

相关文章:

  • 【Vue】Vue3 的初始化过程
  • React Native实现推送通知
  • 免费使用!通过API将文字转换为拼音
  • 15分钟学Python 第22天 :继承与多态
  • 铨顺宏科技携RTLS+RFID技术亮相工博会!
  • Python--循环
  • 数组组成的最小数字 - 华为OD统一考试(E卷)
  • OmniAns丨OPENAIGC开发者大赛高校组AI创作力奖
  • 即插即用篇 | DenseNet卷土重来! YOLOv8 引入全新密集连接卷积网络 | ECCV 2024
  • CentOs-Stream-9 解决此系统未向授权服务器注册问题
  • 行情叠加量化,占据市场先机!
  • 计算机网络1
  • 【数据结构】假设二叉树采用二叉链表存储,编写一棵二又树中序遍历的非递归算法。
  • Apache技术深度解析与实战案例
  • Pydantic 是一个强大的 Python 库
  • EVM理解:深入理解EVM的运作方式,包括Gas机制、交易执行流程等。
  • 【IOS】申请开发者账号(公司)
  • C++ 排序算法
  • 基于51单片机的方向盘模拟系统
  • OJ在线评测系统 后端 使用代理模式编写测试类 并 实现核心业务判题流程
  • 开源治理聚光灯 | 企业规模不同,治理方式各显神通
  • 【openwrt-21.02】VPN Passthrough系列之L2TP Passthrough实现
  • 谷神后端$vs.dbTools.list
  • Windows安装Vim,并在PowerShell中直接使用vim
  • 【裸机装机系列】16.kali(ubuntu)-安装linux和win双系统-重装win11步骤
  • React Native中如何调用iOS的Face ID和Android的生物识别,react-native-biometrics
  • 【深度学习】04-Cnn卷积神经网络-01- 卷积神经网络概述/卷积层/池化层/分类案例精讲
  • 【MySQL】数据库--索引
  • 未来数字世界相关技术、应用:AR/VR/MR;数字人、元宇宙、全息显示
  • 开源链动 2+1 模式 S2B2C 商城小程序:激活 KOC,开启商业新征程