安卓开发第一次实验

中山大学数据科学与计算机学院本科生实验报告

(2018年秋季学期)

课程名称 手机平台应用开发 任课老师 郑贵锋
年级 2016级 专业(方向) 电子政务
学号 16340211 姓名 王广浩
电话 17620124723 Email 1299927852
开始日期 2018.10.1 完成日期 2018.10.9

一、实验题目

基本的UI界面设计


二、实现内容

实现一个Android应用,界面呈现如图中的效果。
安卓开发第一次实验

要求

  • 该界面为应用启动后看到的第一个界面。
  • 各控件的要求
    1. 标题字体大小20sp,与顶部距离20dp,居中;
    2. 图片与上下控件的间距均为20dp,居中;
    3. 输入框整体距左右屏幕各间距20dp,内容(包括提示内容)如图所示,内容字体大小18sp
    4. 按钮与输入框间距10dp,文字大小18sp。按钮背景框左右边框与文字间距10dp,上下边框与文字间距5dp,圆角半径180dp,背景色为**#3F51B5**;
    5. 四个单选按钮整体居中,与输入框间距10dp,字体大小18sp,各个单选按钮之间间距10dp,默认选中的按钮为第一个。

使用的组件

TextView、EditText、ConstraintLayout、Button、ImageView、RadioGroup、RadioButton。


三、课堂实验结果

(1)实验截图

安卓开发第一次实验
安卓开发第一次实验
安卓开发第一次实验

(2)实验步骤以及关键代码

首先在布局文件中创建一个新的XML文件,然后安装指导中的,采用约束布局,先做最顶层的标题和图片


   	android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="中山大学智慧健康服务平台"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:src="@mipmap/sysu"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title" />

之后是是搜索框还有搜索按钮

	<EditText
        android:id="@+id/text1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:hint="请输入搜索内容"
        android:textSize="18sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/button"
        app:layout_constraintTop_toBottomOf="@id/image" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:text="搜索"
        android:textColor="#FFFFFF"
        android:background="@drawable/buttonstyle"
        android:textSize="18sp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/text1"
        app:layout_constraintTop_toBottomOf="@id/image" />
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#3F51B5"/>
    <corners android:radius="180dp"/>
    <padding
        android:top="5dp"
        android:bottom="5dp"
        android:left="10dp"
        android:right="10dp"/>
</shape>

还有组单选按钮

<RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/text1"
        android:layout_marginTop="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/button1"
            style="@style/myButton"
            android:checked="true"
            android:text="图片"
            android:layout_marginLeft="10dp" />
        <RadioButton
            android:id="@+id/button2"
            style="@style/myButton"
            android:text="视频" />
        <RadioButton
            android:id="@+id/button3"
            style="@style/myButton"
            android:text="问答" />
        <RadioButton
            android:id="@+id/button4"
            style="@style/myButton"
            android:text="资讯" />
</RadioGroup>
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/gray</item>

    </style>
    <style name="myButton">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">18sp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_marginRight">10dp</item>
    </style>
</resources>

(3)实验遇到的困难以及解决思路

实验中首先遇到的问题就是如何实现空出20dp,直接想到的是直接规定标题的位置,然后发现使用约束布局没有办法指定绝对位置,然后在手册中发现layout_marginTop可以解决这个问题。之后就基本没有大的问题,最后看到自己的界面做好之后,下划线竟然是粉色,看的很别扭,之后在网上找了教程修改了一下。另外安卓自带的虚拟机太卡了,最后还是用的真机。


四、课后实验结果

安卓开发第一次实验


五、实验思考及感想

本次实验主要是基本的UI设计,熟悉安卓中的基本的控件,与java不同的是,采用了XML来生成布局,可以使用自己定义的风格,颜色,字符串,形状等,同时代码看起来很整齐,可以很直接的就能找到代码出错的地方。这次实验主要是对控件的位置以及控件中字体进行相应的操作,符合所给的要求,做出来还是很美观的。