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

 

 

buttr.xml 设置button的变色

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <item android:state_pressed="true">  
  4. <shape android:shape="rectangle">  
  5. <solid android:color="#E12626"/>  
  6. <corners android:radius="10dp"/>  
  7. </shape>  
  8. </item>  
  9. <item android:state_pressed="false">  
  10. <shape android:shape="rectangle">  
  11. <corners android:radius="10dp"/>  
  12. <stroke android:color="@color/black"/>  
  13. <stroke android:width="2dp"/>  
  14. <solid android:color="#FFFFFF"/>  
  15. </shape>  
  16. </item>  
  17. </selector>  

 

 

 

 

 

textedit_2.xml 文本框变色

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <item android:state_pressed="true">  
  4. <shape android:shape="rectangle">  
  5. <stroke android:width="5dp"/>  
  6. <corners android:radius="10dp"/>  
  7. <solid android:color="@color/white"/>  
  8. <stroke android:color="#03A9F4"/>  
  9. </shape>  
  10. </item>  
  11. <item android:state_pressed="false">  
  12. <shape android:shape="rectangle">  
  13. <corners android:radius="6dp"/>  
  14. <solid android:color="@color/white" />  
  15. <stroke android:color="#B1B0B0"/>  
  16. <size android:width="2dp"/>  
  17. </shape>  
  18. </item>  
  19. </selector>  

 

tectcolor.xml 按钮中颜色的改变

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <item android:state_pressed="true" android:color="@color/yellow"/>  
  4. <item android:color="@color/black"/>  
  5. </selector>  

 

 

 

 

 

 

 

 

 

 

 

 activity_main2.xml

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

 

radiusrl.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:shape="rectangle">  
  4. <corners android:topRightRadius="50dp"  
  5. android:bottomLeftRadius="50dp"/>  
  6. </shape>  

 

      

 

3.

 

 

 

 

 

 

activity_main3.xml

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

 

 

edit.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:shape="rectangle">  
  4. <stroke android:width="2dp"  
  5. android:color="#9F9E9E"/>  
  6. <size android:height="50dp"/>  
  7. <corners android:radius="30dp"/>  
  8. </shape>  

 

 

tv_3.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:shape="rectangle">  
  4. <size android:height="100dp"  
  5. android:width="100dp"/>  
  6. <corners android:radius="20dp"/>  
  7. </shape>  

 

 

pup.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:shape="rectangle">  
  4. <corners android:topLeftRadius="50dp"  
  5. android:bottomRightRadius="50dp"/>  
  6. <solid android:color="#9C27B0"/>  
  7. <size android:height="260dp"/>  
  8. </shape>  

 

posted @ 2022-03-08 17:11  Eveeee  阅读(411)  评论(0)    收藏  举报