Android家庭记账本开发第二天:activity_main布局文件

本次开发记录是在开发完成之后记录的,所以能写几篇我也不知道,可能没有十篇,将这次的开发记录作为一次知识点复习的机会。

开发完成之后的目录如下

安卓开发相较于之前进行的Javaweb开发难度要高上不少,因为之前的都是通过tomcat服务器去运行,我们只需要将对应的页面完善好就行,但是安卓开发相较于web开发有很大不同,布局页面,适配器,activity和数据库操作都有很大不同,本次记录先从页面布局入手。

安卓开发的页面布局文件是放在res/layout目录下的xml文件,xml(可扩展标记语言)用于定义文档的结构和内容。在安卓开发中,XML被广泛用于定义布局、样式、字符串等资源。

 一个布局文件中可以包含如下多种元素:

  1. 布局容器:布局文件中通常包含一个或多个布局容器,用于组织和排列界面中的各个视图组件。常见的布局容器包括LinearLayout、RelativeLayout、ConstraintLayout等,它们分别提供了不同的布局方式和特性。

  2. 视图组件:视图组件是指界面上的各种元素,如按钮、文本框、图片等。在布局文件中,通过在布局容器中添加视图组件来构建界面的外观和交互。

  3. 布局属性:每个视图组件都可以设置一系列布局属性,用于控制其在布局中的位置、大小、样式等。这些属性可以在XML中通过标签的属性来指定,也可以在Java代码中动态设置。

可以通过右键新建activity>empty views activity进行新建,在新建之后会在Java目录下生成一个Java文件和一个与之对应的在res/layout目录下的xml文件。

 在项目新建时会自动生成一个mainactivity文件和一个与之对应的activity_main文件,这个就是我们程序打开之后的页面,xml文件负责布局而Java文件负责对应的逻辑操作。

这里我们先来看一下我的软件的mainxml布局文件代码:

复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/buttons"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp" />

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/refresh"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_below="@+id/list_view"
            android:layout_margin="20dp"
            android:layout_marginLeft="20dp"
            android:layout_toLeftOf="@id/add"
            android:background="#FFFFFF"
            android:contentDescription="TODO"
            android:onClick="refresh"
            android:scaleType="centerInside"
            android:src="@drawable/refresh"
            tools:ignore="ContentDescription" />

        <ImageButton
            android:id="@+id/add"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_below="@+id/list_view"
            android:layout_centerHorizontal="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:background="#FFFFFF"
            android:contentDescription="TODO"
            android:onClick="addAccount"
            android:scaleType="centerInside"
            android:src="@drawable/tianjia"
            tools:ignore="ContentDescription" />

        <ImageButton
            android:id="@+id/search"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_below="@+id/list_view"
            android:layout_margin="20dp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/add"
            android:background="#FFFFFF"
            android:contentDescription="TODO"
            android:onClick="searchAccount"
            android:scaleType="centerInside"
            android:src="@drawable/search"
            tools:ignore="ContentDescription" />

    </LinearLayout>
    </LinearLayout>
</RelativeLayout>
复制代码

效果如下:

这里的最外层标签是RelativeLayout(相对布局容器):

  • RelativeLayout允许通过相对位置来排列子视图,子视图可以相对于父容器或其他子视图进行定位。
  • 可以使用诸如android:layout_aboveandroid:layout_belowandroid:layout_toStartOfandroid:layout_toEndOf等属性来指定子视图的位置关系。
  • 这种布局容器适用于需要根据其他视图组件的位置来动态排列的情况,如相对位置不固定的情况下进行布局设计。

其中xmlns:androidxmlns:appxmlns:tools 是用来定义 XML 命名空间的属性。这些属性用于告诉解析器如何解释和处理 XML 中的元素和属性。

  1. xmlns:android="http://schemas.android.com/apk/res/android":

    • xmlns:android 声明了一个 XML 命名空间,其值指定了 Android 系统提供的默认命名空间的 URI(Uniform Resource Identifier)。
    • 这个命名空间包含了 Android 框架中定义的所有标准属性和元素。通过将布局文件与 Android 系统的默认命名空间关联,可以在布局文件中使用 Android 提供的标准属性和元素。
  2. xmlns:app="http://schemas.android.com/apk/res-auto":

    • xmlns:app 声明了一个 XML 命名空间,其值指定了应用程序包名的 URI。
    • 在 Android 开发中,通常使用 app 命名空间来引用自定义属性,特别是在使用支持库(Support Library)中的组件时。支持库中的某些属性可能无法通过默认的 Android 命名空间引用,因此需要使用 app 命名空间来引用这些属性。
  3. xmlns:tools="http://schemas.android.com/tools":

    • xmlns:tools 声明了一个 XML 命名空间,其值指定了 Android 开发工具的命名空间的 URI。
    • 这个命名空间用于在开发过程中提供工具支持,例如在设计界面时可以使用 tools 命名空间中的属性来预览布局效果,而不会影响实际运行时的布局。常见的属性包括tools:context,用于指定与布局相关联的 Activity,以便在设计时显示上下文信息。

而android:layout都是和布局相关的属性,这里不在细说,给出一条链接参考:Android layout属性大全_layout_tostartof-CSDN博客

下面的ListView是安卓开发中常用的列表视图控件,用于展示垂直滚动的可变长度的数据列表。ListView通过与适配器(Adapter)配合使用来提供数据。适配器负责将数据与每个列表项进行绑定,以便正确渲染和展示。常用的适配器包括ArrayAdapter、CursorAdapter和BaseAdapter等。

通过适配器向ListView添加数据,可以使用适配器的方法(如add()addAll())添加单个或多个数据项。一旦数据被添加到适配器,ListView会自动刷新并显示新数据。\

可以为ListView的列表项设置点击事件监听器,使用户能够对列表项进行交互操作。通过实现OnItemClickListener接口,可以处理列表项的点击事件,并执行相应的逻辑操作。

以上是软件会利用到的一些知识,具体参考链接【Android从零单排系列二十】《Android视图控件——ListView》_android listview-CSDN博客

下面给出用于列表显示数据的xml文件:

复制代码
<?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="wrap_content"
    android:orientation="horizontal"
    android:clickable="true"
    android:focusable="true"
    android:background="?android:attr/selectableItemBackground">

    <TextView
        android:id="@+id/tv_title"
        android:layout_margin="10dp"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginStart="10dp"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textSize="20sp"
        android:text="costTitle" />

    <TextView
        android:id="@+id/tv_date"
        android:layout_margin="10dp"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5"
        android:gravity="center_vertical"
        android:textSize="18sp"
        android:layout_marginStart="10dp"
        android:text="2020-05-31"/>

    <TextView
        android:id="@+id/tv_money"
        android:layout_margin="10dp"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:layout_marginEnd="10dp"
        android:textSize="25sp"
        android:textColor="#ffbd27"
        android:text="25"/>
</LinearLayout>
复制代码

对于布局相关的属性不再细说,主要注意:

android:clickable="true" 让数据项可以点击,通过设置监听器可对数据进一步操作
android:layout_weight="1" 通过设置权重可以在不同分辨率页面上按权重显示
match_partent属性可以让大小和父类布局相匹配

之后就是两个LinearLayout嵌套的三个按钮

按钮标签很好理解,不再细说,设置好id,图片和点击触发函数即可。

两个LinearLayout的嵌套主要是为了解决在不同分辨率的设备下的显示问题,里层的标签是为了让三个按钮居中对齐,而第二层则是在垂直方向性上的一个LinearLayout布局,这样就可以在ListView中设置一个android:layout_above="@id/buttons"属性,以使其达到一个相对布局的目的。

这里的布局文件先介绍到这里,如有说错的地方还请多多包含,代码肯定不是最优解,还请不要有太高要求,只是作为学习的一次记录。

posted @   起名字真难_qmz  阅读(78)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示