2.1 Concept of View and ViewGroup
Category Android Basic Introduction Tutorial
Introduction to This Section
Having bid farewell to the first chapter, we now embark on the second chapter, which delves into the detailed explanation of UI (User Interface) components in Android. In this section, we will focus on the parent classes of all controls, View and ViewGroup! Inspired by a sudden thought, let's directly translate the official documentation's introduction to these two concepts. By the way, due to restrictions in China, accessing Google and Android developer sites is not possible. We can modify the hosts file or use a VPN, or alternatively, use a domestic API mirror, such as this one: http://androiddoc.qiniudn.com/guide/topics/ui/overview.html, which mirrors the API for version 5.0!
UI Overview
In Android apps, all user interface elements are composed of View and ViewGroup objects. A View is an object that draws on the screen and with which users can interact. A ViewGroup, on the other hand, is a container that holds other View (and ViewGroup) objects, acting as a layout container! Android provides us with a collection of View and ViewGroup subclasses, including common input controls (like buttons and text fields) and various layout patterns (like linear or relative layouts).
User Interface Layout
Each component on your app's user interface is constructed using a hierarchy of View and ViewGroup objects, as shown in Figure 1. Each ViewGroup is an invisible container that organizes child Views, which could be input controls or widgets that draw a specific area on the UI. With a hierarchy tree, you can design simple or complex layouts according to your needs (simpler layouts perform better).
To define your layout, you can instantiate View objects in code and start building your tree, but the easiest and most efficient way to define your layout is to use an XML file. XML layout is more human-readable, similar to HTML.
The name of the XML element represents a View. So, a <TextView>
element creates a TextView control in your interface, while a <LinearLayout>
creates a LinearLayout container.
For example, a simple vertical layout with a text view and a button looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Button" />
</LinearLayout>
When your app loads the above layout resource, Android instantiates each node in the layout into objects, and you can define additional behaviors, query the state of these objects, or modify the layout. For a complete guide to creating UI layouts, refer to XML Layouts.
User Interface Components
You don't need to create your UI layout entirely with View and ViewGroup objects. Android provides several standard UI layouts that you can define content for. These UI components come with API documentation that introduces their attributes, such as the action bar, dialogs, and status notification bars.
Summary of This Section
Well, the translation might be a bit awkward. Alas, as someone with limited English proficiency, I've done my best. To summarize the above content simply:
>
Graphical interfaces in Android are composed of View and ViewGroup and their subclasses: View: The parent class of all visual controls, providing methods for component rendering and event handling. ViewGroup: A subclass of the View class that can contain child controls, acting as a container. Android UI controls are stacked in a hierarchical tree structure, and there are two ways to create UI layouts: writing code in Java or defining layouts in XML, with the latter being more convenient and easier to understand! It's also the most commonly used method. Additionally, we rarely use View and ViewGroup directly to create layouts; instead, we usually use their subclass controls or containers to build layouts.
So, having a general understanding of View and ViewGroup is sufficient. We usually don't use them directly; they are typically used when customizing views.
-1.0 Android Basic Introduction Tutorial
-1.2.1 使用Eclipse + ADT + SDK开发Android APP
-1.2.2 使用Android Studio开发Android APP
- 2.1 View与ViewGroup的概念
-2.3.3 Button(按钮)与ImageButton(图像按钮)
-2.3.5.RadioButton(单选按钮)&Checkbox(复选框)
-2.3.6 开关按钮ToggleButton和开关Switch
-2.4.8 ListView之checkbox错位问题解决
- 2.5.2 Basic Usage of GridView (Grid View)
- 2.5.3 Basic Usage of Spinner (Dropdown List)
- 2.5.4 Basic Usage of AutoCompleteTextView (Auto-complete Text Field)
- 2.5.5 Basic Usage of ExpandableListView (Expandable List)
- 2.5.6 Basic Usage of ViewFlipper (Flip View)
- 2.5.7 Basic Usage of Toast (Toast Notification)
- 2.5.8 Detailed Guide on Notification (Status Bar Notification)
- 2.5.9 Detailed Guide on AlertDialog (Dialog Box)
- 2.6.0 Basic Usage of Other Common Dialogs
- 2.6.1 Basic Usage of PopupWindow (Floating Window)
- 2.6.2 Menu (Menu)
- 2.6.3 Simple Usage of ViewPager
- 2.6.4 Simple Usage of DrawerLayout (Official Side Sliding Menu)
- 3.1.1 Event Handling Mechanism Based on Listeners
- 3.2 Event Handling Mechanism Based on Callbacks
- 3.3 Analysis of Handler Message Passing Mechanism
- 3.4 TouchListener vs OnTouchEvent + Multi-touch
- 3.5 Listening for Content Changes in EditText
- 3.6 Responding to System Setting Changes (Configuration Class)
- 3.7 AsyncTask Asynchronous Tasks
- 3.8 Gestures (Gestures)
- 4.1.1 Introduction to Activity
- 4.1.2 Getting Started with Activity
- 4.1.3 Advanced Activity
- 4.2.1 Introduction to Service
- 4.2.2 Advanced Service
- 4.2.3 Expert Service
- 4.3.1 Basic BroadcastReceiver
- 4.3.2 Detailed BroadcastReceiver
- 4.4.1 Introduction to ContentProvider
- 4.4.2 Advanced ContentProvider - Document Provider
- 4.5.1 Basic Usage of Intent
- 4.5.2 Passing Complex Data with Intent
- 5.1 Basic Overview of Fragment
- 5.2.1 Fragment Example - Bottom Navigation Bar Implementation (Method 1)
- 5.2.2 Fragment Example - Bottom Navigation Bar Implementation (Method 2)
- 5.2.3 Fragment Example - Bottom Navigation Bar Implementation (Method 3)
- 5.2.4 Fragment Example - Bottom Navigation Bar + ViewPager Page Swiping
- 5.2.5 Fragment Example - Simple Implementation of News/Shopping App List Fragment
- 6.1 Data Storage and Access - File Storage and Reading
- 6.2 Data Storage and Access - SharedPreferences for Saving User Preferences
- 6.3.1 Data Storage and Access - Introduction to SQLite Database
- 6.3.2 Data Storage and Access - Advanced SQLite Database
- 7.1.1 Android Network Programming and HTTP Protocol Learning
- 7.1.2 Android HTTP Request and Response Headers Learning
- 7.1.3 Android HTTP Request Method: HttpURLConnection
- 7.1.4 Android HTTP Request Method: HttpClient
- 7.2.1 Android XML Data Parsing
- 7.2.2 Android JSON Data Parsing
- 7.3.1 Android File Upload
- 7.3.2 Android File Download (1)
- 7.3.3 Android File Download (2)
- 7.4 Android WebService Calling
- 7.5.1 Basic Usage of WebView (Web View)
- 7.5.2 WebView and JavaScript Interaction Basics
- 7.5.3 WebView Considerations After Android 4.4
- 7.5.4 WebView File Download
- 7.5.5 WebView Cache Issues
- 7.5.6 WebView Handling Webpage Error Codes
- 7.6.1 Socket Network Basics Preparation
- 7.6.2 TCP Protocol Based Socket Communication (1)
- 7.6.3 TCP Protocol Based Socket Communication (2)
- 7.6.4 UDP Protocol Based Socket Communication
- 8.1.1 Summary of 13 Drawable Types in Android Part 1
- 8.1.2 Summary of 13 Drawable Types in Android Part 2
- 8.1.3 Summary of 13 Drawable Types in Android Part 3
- 8.2.1 Comprehensive Guide to Bitmap (Bitmap) Part 1
- 8.2.2 OOM Issues Caused by Bitmap
- 8.3.1 Detailed Explanation of Three Drawing Tools
- 8.3.2 Drawing Tool Practical Examples
- 8.3.3 Paint API - MaskFilter (Mask)
- 8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 1)
- 8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 2)
- 8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 3)
- 8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 4)
- 8.3.8 Paint API - Xfermode and PorterDuff Explained (Part 5)
8.3.14 Paint Enumerations/Constants and ShadowLayer Shadow Effects
8.3.18 Canvas API Explained (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - Introduction
8.4.4 Android Animation Collection - Property Animation - Further Exploration
12.1 Android Practice: DrySister App (Version 1) - Project Setup and Basic Implementation
12.3 DrySister App (Version 1) - Image Loading Optimization (Building an Image Cache Framework)
12.4 DrySister App (Version 1) - Adding Data Caching (Introducing SQLite)
12.5 DrySister App (Version 1) - Code Review, Adjustments, and Logging Class
12.6 DrySister App (Version 1) - Icon Creation, Obfuscation, Signing, APK Slimming, App Release