android studio实验1


实验名称

资源文件的使用

实验序号

实验一

   

 

系院专业

软件工程

   

 

   

 

实验日期

2022/3/7

指导教师

 

一、实验目的和要求

1.  掌握资源文件的定义

2.  掌握资源文件的两种使用方法(XMLJava

二、实验内容

  1. 给定App界面和要求,设计布局文件,灵活运用资源文件,实现不同效果。

三、实验项目

  1. 根据下图,设计布局文件:
    1) 界面元素的效果如图所示。包括字体大小、对齐方式、控件的间隔、按钮的外观。
    2) 点击按钮时,要求按钮的底色是红色,字体颜色是黄色。
    3) 点击输入框时,要求它的边框线颜色是加粗的蓝色圆角框。不点击输入框时,要求它的边框线是细的灰色圆角框。如图1所示。

4要求分别用相对布局来实现。

提交的内容为:

(1) 新建代码文件、修改的代码文件内容列在实验报告的“四、实验结果”处

(2) 运行初始界面截图,点击按钮时的截图、点击输入框时的截图列在实验报告的“四、实验结果”处

   1

  1. 根据下图,设计布局文件

(1) 界面元素的效果如图2所示,包括形状、间隔、对齐方式。

2要求分别用线性布局来实现。

 

提交的内容为:

(1) 新建代码文件、修改的代码文件内容列在实验报告的“四、实验结果”处。

(2) 运行初始界面截图,点击图片时的截图列在实验报告的“四、实验结果”处。

2

  1. 根据下图,设计布局文件

(1) 界面元素的效果如图3所示,包括字体大小、形状、间隔、对齐方式。

(2) 要求用约束布局来实现

 

提交的内容为:

(1) 新建代码文件、修改的代码文件内容列在实验报告的“四、实验结果”处。

(2) 运行画面的截图列在实验报告的“四、实验结果”处。

3

四、实验程序、结果

1

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. xmlns:app="http://schemas.android.com/apk/res-auto"  
  3. xmlns:tools="http://schemas.android.com/tools"  
  4. android:layout_width="match_parent"  
  5. android:layout_height="match_parent"  
  6. tools:context=".MainActivity"  
  7. android:background="#DDDBDB"  
  8. android:padding="10dp">  
  9. android:id="@+id/tv_es"  
  10. android:layout_width="wrap_content"  
  11. android:layout_height="wrap_content"  
  12. android:layout_centerHorizontal="true"  
  13. android:layout_marginVertical="100dp"  
  14. android:text="你确定要退出吗?"  
  15. android:textColor="#9E9898"  
  16. android:textSize="30sp"  
  17. android:textStyle="bold" />  
  18. android:id="@+id/relaout"  
  19. android:layout_width="match_parent"  
  20. android:layout_height="wrap_content"  
  21. android:layout_below="@id/tv_es"  
  22. android:background="#00BCD4"  
  23. android:gravity="center_horizontal"  
  24. android:padding="15dp">  
  25. android:id="@+id/bt_1"  
  26. android:layout_width="wrap_content"  
  27. android:layout_height="wrap_content"  
  28. android:background="@drawable/buttr"  
  29. android:text="确定"  
  30. android:textColor="@color/tectcolor"  
  31. android:textSize="30sp" />  
  32. android:id="@+id/bt_2"  
  33. android:layout_width="wrap_content"  
  34. android:layout_height="wrap_content"  
  35. android:layout_marginLeft="50dp"  
  36. android:layout_toRightOf="@id/bt_1"  
  37. android:background="@drawable/buttr"  
  38. android:text="取消"  
  39. android:textColor="@color/tectcolor"  
  40. android:textSize="30sp" />  
  41.   
  42. android:id="@+id/tv_1"  
  43. android:layout_width="wrap_content"  
  44. android:layout_height="wrap_content"  
  45. android:layout_below="@id/relaout"  
  46. android:layout_marginStart="15dp"  
  47. android:layout_marginTop="50dp"  
  48. android:text="旧密码:"  
  49. android:textColor="#9E9898"  
  50. android:textSize="30sp"  
  51. android:textStyle="bold" />  
  52. android:id="@+id/et_1"  
  53. android:layout_width="match_parent"  
  54. android:layout_height="wrap_content"  
  55. android:layout_below="@id/relaout"  
  56. android:layout_alignBaseline="@id/tv_1"  
  57. android:layout_marginTop="50dp"  
  58. android:layout_toEndOf="@id/tv_1"  
  59. android:background="@drawable/textedit_2"  
  60. android:minHeight="55dp"  
  61. tools:ignore="SpeakableTextPresentCheck" />  
  62. android:id="@+id/tv_2"  
  63. android:layout_width="wrap_content"  
  64. android:layout_height="wrap_content"  
  65. android:layout_below="@id/relaout"  
  66. android:layout_marginStart="15dp"  
  67. android:layout_marginTop="125dp"  
  68. android:text="新密码:"  
  69. android:textColor="#9E9898"  
  70. android:textSize="30sp"  
  71. android:textStyle="bold" />  
  72. android:id="@+id/et_2"  
  73. android:layout_width="match_parent"  
  74. android:layout_height="wrap_content"  
  75. android:layout_below="@id/relaout"  
  76. android:layout_alignBaseline="@id/tv_2"  
  77. android:layout_marginTop="50dp"  
  78. android:layout_toEndOf="@id/tv_2"  
  79. android:background="@drawable/textedit_2"  
  80. android:minHeight="55dp"  
  81. tools:ignore="SpeakableTextPresentCheck" />  
  82.   

buttr.xml 设置button的变色

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.   
  4.   
  5.   
  6.   
  7.   
  8.   
  9.   
  10.   
  11.   
  12.   
  13.   
  14.   
  15.   
  16.   
  17.   

textedit_2.xml 文本框变色

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.   
  4.   
  5.   
  6. 10dp"/>  
  7.   
  8.   
  9.   
  10.   
  11.   
  12.   
  13.   
  14.   
  15.   
  16.   
  17.   
  18.   
  19.   

tectcolor.xml 按钮中颜色的改变

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.   
  4.   
  5.   

 activity_main2.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. xmlns:app="http://schemas.android.com/apk/res-auto"  
  3. xmlns:tools="http://schemas.android.com/tools"  
  4. android:layout_width="match_parent"  
  5. android:layout_height="match_parent"  
  6. tools:context=".MainActivity2"  
  7. android:orientation="horizontal">  
  8. android:layout_width="match_parent"  
  9. android:layout_height="match_parent"  
  10. android:orientation="vertical"  
  11. android:layout_marginRight="10dp"  
  12. android:layout_weight="2"  
  13. android:padding="5dp"  
  14. >  
  15. android:layout_width="match_parent"  
  16. android:layout_height="match_parent"  
  17. android:layout_weight="1"  
  18. android:layout_marginBottom="10dp"  
  19. android:backgroundTint="@color/purple_700"  
  20. android:background="@drawable/radiusrl" />  
  21. android:layout_width="match_parent"  
  22. android:layout_height="match_parent"  
  23. android:layout_weight="1"  
  24. android:backgroundTint="#4CAF50"  
  25. android:background="@drawable/radiusrl"  
  26. android:layout_marginBottom="10dp" />  
  27. android:layout_width="match_parent"  
  28. android:layout_height="match_parent"  
  29. android:layout_weight="1"  
  30. android:backgroundTint="#61E8C1"  
  31. android:background="@drawable/radiusrl" />  
  32.   
  33. android:layout_width="match_parent"  
  34. android:layout_height="match_parent"  
  35. android:layout_weight="2"  
  36. android:orientation="vertical"  
  37. android:padding="5dp">  
  38. android:layout_width="match_parent"  
  39. android:layout_height="match_parent"  
  40. android:backgroundTint="@color/yellow"  
  41. android:background="@drawable/radiusrl"  
  42. android:layout_marginBottom="10dp"  
  43. android:layout_weight="2" />  
  44. android:layout_width="match_parent"  
  45. android:layout_height="match_parent"  
  46. android:backgroundTint="#E44338"  
  47. android:background="@drawable/radiusrl"  
  48. android:layout_weight="1" />  
  49.   
  50.   

radiusrl.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. android:shape="rectangle">  
  3. android:bottomLeftRadius="50dp"/>  
  4.   

3.

 

 

activity_main3.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. xmlns:app="http://schemas.android.com/apk/res-auto"  
  3. xmlns:tools="http://schemas.android.com/tools"  
  4. android:layout_width="match_parent"  
  5. android:layout_height="match_parent"  
  6. tools:context=".MainActivity3"  
  7. android:padding="30dp">  
  8. android:id="@+id/tv_1"  
  9. android:layout_width="wrap_content"  
  10. android:layout_height="wrap_content"  
  11. android:text="说走就走的旅行"  
  12. android:textSize="35sp"  
  13. android:textStyle="bold"  
  14. android:textColor="#817F7F"  
  15. android:paddingVertical="10dp"  
  16. tools:ignore="MissingConstraints" />  
  17. android:id="@+id/ed_1"  
  18. android:layout_width="match_parent"  
  19. android:layout_height="wrap_content"  
  20. android:layout_marginTop="10dp"  
  21. app:layout_constraintTop_toBottomOf="@+id/tv_1"  
  22. android:background="@drawable/edit"/>  
  23. android:id="@+id/tv_3"  
  24. android:layout_width="wrap_content"  
  25. android:layout_height="wrap_content"  
  26. android:text="欧洲"  
  27. android:gravity="center"  
  28. android:layout_marginTop="20dp"  
  29. android:backgroundTint="#2196F3"  
  30. android:background="@drawable/tv_3"  
  31. app:layout_constraintTop_toBottomOf="@+id/ed_1"  
  32. tools:ignore="MissingConstraints" />  
  33. android:id="@+id/tv_4"  
  34. android:layout_width="wrap_content"  
  35. android:layout_height="wrap_content"  
  36. android:text="欧洲"  
  37. android:gravity="center"  
  38. android:layout_marginTop="20dp"  
  39. android:layout_marginStart="25dp"  
  40. app:layout_constraintStart_toEndOf="@+id/tv_3"  
  41. android:backgroundTint="#FFEB3B"  
  42. android:background="@drawable/tv_3"  
  43. app:layout_constraintTop_toBottomOf="@+id/ed_1"/>  
  44. android:layout_width="wrap_content"  
  45. android:layout_height="wrap_content"  
  46. android:text="欧洲"  
  47. android:gravity="center"  
  48. android:layout_marginTop="20dp"  
  49. android:layout_marginStart="25dp"  
  50. app:layout_constraintStart_toEndOf="@+id/tv_4"  
  51. android:backgroundTint="#9C3515"  
  52. android:background="@drawable/tv_3"  
  53. app:layout_constraintTop_toBottomOf="@+id/ed_1"/>  
  54. android:id="@+id/tv_5"  
  55. android:layout_width="wrap_content"  
  56. android:layout_height="wrap_content"  
  57. android:text="受欢迎的旅游景点"  
  58. android:textSize="35sp"  
  59. android:textStyle="bold"  
  60. android:textColor="#817F7F"  
  61. android:paddingVertical="20dp"  
  62. app:layout_constraintTop_toBottomOf="@id/tv_4"  
  63. tools:ignore="MissingConstraints" />  
  64. android:layout_width="match_parent"  
  65. android:layout_height="wrap_content"  
  66. app:layout_constraintTop_toBottomOf="@id/tv_5"  
  67. android:background="@drawable/pup"/>  
  68.   

edit.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. android:shape="rectangle">  
  3. android:color="#9F9E9E"/>  
  4.   
  5.   
  6.   

tv_3.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. android:shape="rectangle">  
  3. android:width="100dp"/>  
  4.   
  5.   

pup.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. android:shape="rectangle">  
  3. android:bottomRightRadius="50dp"/>  
  4.   
  5.   
  6.