Android Study Notes 1.2
one
- After installing the Android integrated development environment, we must learn to create Android applications and understand the MVC architecture of Android applications - control layer: write code in MainActivity to control program logic, view layer: design the user interface in activity_main, and model layer: strings. Define string data in xml.
(1) Create Android applications
-
Create a new Android project
-
Based on
Empty Activity
template -
Configure basic project information
-
Click the [Finish] button
(2) Running Android applications
- Click the green run button on the toolbar
- Start the simulator and view the program running results
- There is a label in the center of the screen -
Hello World
(3) Set screen labels
1. Modify label text attributes
-
Open the res\layout directory and open the main layout file
activity_main.xml
. The default is Design view.
-
Switch to Code view
-
Modify
android:text
attribute value
-
Restart the application and see the effect
-
Looking at the screen prompts, it is recommended not to use hardcoded strings (Hardcoded string) and should use @string resources.
-
So we need to define a string variable in the file
res/value
in the directorystrings.xml
welcome
-
activity_main.xml
Call this string variable in the layout configuration filewelcome
2. Set the label font size and color
- Modify label font size and color, textSize - text font size; textColor - text color
- Restart the app and see the effect
3. Modify the application title
- Find the Android app we created in the app list -
Test
- The program runs and the title on the activity bar is also the name of the application -
Test
app_name
Modify the value of a string variable in a string resource file
- Restart the app and see the effect
- Close the app and view the app list
(4) Modify the layout and set layout properties
1. Change constraint layout to linear layout
- Change ConstraintLayout to LinearLayout
2. Set linear layout properties
(1) Set background image
-
Official documentation: Ordinary pictures are placed in the drawable directory, and icons are placed in the mipmap directory.
-
Copy the background image background.png to the drawable directory
-
background
Set properties for linear layout
-
Run the program and see the effect
(2) Set the alignment of sub-controls
-
Set the linear layout
gravity
properties tocenter
-
Run to see the effect
(3) Set the linear layout direction
- If you want to add multiple label controls to a linear layout, the default is to arrange them horizontally, that is, the orientation property of the linear layout defaults to horizontal. If you want to arrange vertically, then the orientation attribute of the linear layout must be set to vertical.
- Add a label and set related attributes
- Run the program and see the effect
- If you want the two labels to be separated by a little, then set the margin attributes (marginLeft, marginRight, marginTop, marginBottom)