XML layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/true_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button" /> <Button android:id="@+id/false_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/pre_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pre_button" android:drawableLeft="@drawable/arrow_left" android:drawablePadding="4dp" /> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_button" android:drawableRight="@drawable/arrow_right" android:drawablePadding="4dp" /> </LinearLayout> <Button android:id="@+id/cheat_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cheat_button" /> </LinearLayout>
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-bU51LFyx-1624521261741)(./image-20210512094437931.png)]
assembly
LinearLayout,TextView,Button,...
Components can display text or images to interact with users. Each component is a concrete instance of the View class or its subclasses.
Component properties
-
android:layout_width and android:layout_height
- match_parent: the view is the same size as its parent view
- wrap_content: the view will be automatically resized according to its content
-
android:padding: inner margin
- 24dp: DP indicates density independent pixels
-
android:orientation: placement direction of subcomponents
- Vertical: vertical placement
- horizontal: horizontal
-
android:text: text content to be displayed by the component
- @string/xxx: reference to string resource
Style, theme, theme properties
A style is an XML resource file that contains attribute definitions that describe the behavior and appearance of a component. For example, the following style resources can configure components to display text larger than normal:
<style name="BigTextStyle"> <item name="android:textSize">20sp</item> <item name="android:padding">3dp</item> </style>
You can create your own style file, save it in res/values/values, and then reference it with @ style/xxx in the layout file.
A theme is a collection of styles. Structurally, the theme itself is also a style resource, but its attributes point to other style resources.
Use the theme attribute reference to add predefined application theme styles to the specified components:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/crime_title_label" style="?android:listSeparatorTextViewStyle" />
String resource
string.xml
<resources> <string name="app_name">Knowledge test</string> <!-- MainActivity --> <string name="question_week">The first day of the week is Monday.</string> <string name="question_river">The Nile is the longest river in the world.</string> <string name="question_marriage">The legal age of marriage in China is 20.</string> <string name="question_summer">Summer generally refers to 4~6 Month.</string> <string name="question_lightning">Lightning and thunder are causal.</string> <string name="true_button">correct</string> <string name="false_button">error</string> <string name="pre_button">Last question</string> <string name="next_button">Next question</string> <string name="correct_toast">Correct answer!</string> <string name="incorrect_toast">Wrong answer!</string> <!-- CheatActivity --> <string name="warning_text">Are you sure you want to do this?</string> <string name="show_answer_button">Show answers</string> <string name="cheat_button">cheat</string> <string name="judgment_toast">Cheating is wrong.</string> </resources>
From layout XML to view object
/** * Get the user interface of the activity * @param id int */ setContentView(R.layout.activity_main);
Add resource ID for component
Android generates resource IDs for the entire layout file and individual strings, but activity_ main. Except for the components in the XML layout file, because not all components require a resource ID.
- android:id
- @+id/xxx: add the resource ID for the component, and the identifier is xxx
Actual use of components
Reference component
/** * Reference generated components * @param id int * @return View */ mTrueButton = (Button) findViewById(R.id.true_button);
Set listener
/** * Set the click event listener for the button * @param An object that implements the OnClickListener interface */ mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkAnswer(true); } });
Create prompt message
/** * Create a toast * @param context Context * @param resId int * @param duration int * @return Toast */ Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
Update content for TextView
int questionId = mQuestionBank[mCurrentIndex].getTextResId();mQuestionTextView.setText(questionId);
Add Icon resource
Switch to the Project view, create a new directory drawable MDPI under the res directory, and then add the icon file. When finished, add properties for the required components:
-
android:drawableRight and android:drawableLeft
- @drawable/xxx
-
android:drawablePadding
- 4dp
Activity lifecycle
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-oPFLGUAe-1624521261742)(./image-20210512105836760.png)]
Device rotation and Activity lifecycle
When the device rotates, the system will destroy the current MainActivity instance and create a new MainActivity instance.
Output log information
public static final String TAG = "QuizActivity";Log.d(TAG, "The current question index is " + mCurrentIndex);
Equipment rotation
Create a horizontal mode layout
Right click res directory, new = > Android resource directory, Resource type, select layout, add Orientation to the right, select Landscape option, and click OK.
Copy or create a new activity_main.xml to the layout land directory. Note that the layout file name must be the same. Modify the layout file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="24dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/true_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button" /> <Button android:id="@+id/false_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button" /> </LinearLayout> <Button android:id="@+id/cheat_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:text="@string/cheat_button" /> <Button android:id="@+id/pre_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|left" android:text="@string/pre_button" android:drawableLeft="@drawable/arrow_left" android:drawablePadding="4dp" /> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:text="@string/next_button" android:drawableRight="@drawable/arrow_right" android:drawablePadding="4dp" /></FrameLayout>
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-vAb77QSm-1624521261743)(./image-20210512112826918.png)]
FrameLayout does not arrange the position of its child views in a specific way. The position arrangement of its child views depends on their respective android:layout_gravity property.
Save data before equipment rotation
You can override the Activity method onSaveInstanceState (Bundle):
private static final String KEY_INDEX = "index";/** * Save data (key value pair) in bundle * @ param outState Bundle */@Overrideprotected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.i(TAG, "onSaveInstanceState..."); outState.putInt(KEY_INDEX, mCurrentIndex);}
The onSaveInstanceState(Bundle) method is usually called by the system before the onPause(), onStop(), onDestroy() methods.
Bundle is a structure that stores the mapping relationship (key value pair) between string keys and qualified type values.
When overriding the onCreate(Bundle) method, we are actually calling the onCreate(Bundle) method of the activity parent class and passing in the received bundle. In the implementation of parent code, the view hierarchy of activity can be recreated by taking out the saved view state data.
Get the stored bundle information
if (savedInstanceState != null) mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
Android application debugging
Log
Set breakpoint
Use exception breakpoints
Using Android Lint
Android Lint is a static analyzer for Android application code.
Analyze => Inspect Code => Whole project => OK