Phần biệt View và View group trong Android

In Android Layout is used to describe the user interface for an app or activity, and it stores the UI elements that will be visible to the user. An android app’s user interface is made up of a series of View and ViewGroup elements. In most cases, android apps will have one or more operations, each of which is a single screen of the app. Multiple UI components will be present in the operations, and those UI components will be instances of the View and ViewGroup subclasses. Generally, the android apps will contain one or more activities and each activity is one screen of the app. The activities will contain multiple UI components and those UI components are the instances of View and ViewGroup subclasses. In Android apps, the two very central classes are the Android View class and ViewGroup class. One or more tasks can be found in an Android app. A screen in an Android operation is identical to the windows in a desktop application. GUI components may be used in an operation. View or ViewGroup subclasses are used to build the GUI elements. 

View is a basic building block of UI [User Interface] in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views [child views] and other ViewGroup. Eg: LinearLayout is a ViewGroup that can contain other views in it. ViewGroup is a special kind of view that is extended from View as its base class. ViewGroup is the base class for layouts. As the name states View is singular and the group of Views is the ViewGroup. In simple terms, a view is a user interface feature that we interact with when we use an app, such as a button, editing text and images, and so on. Android.view has a child class called View. Observe While the View group is the container that houses all of these views as well as many other ViewGroup such as linear or Frame Layout. For example, if we design and use the LinearLayout as the root feature, our main layout would be the LinearLayout. Within it, we can add another view category [i.e. another LinearLayout] and several other views such as buttons or TextViews.



View

The View class is the base class or we can say that it is the superclass for all the GUI components in android. For example, the EditText class is used to accept the input from users in android apps, which is a subclass of View, and another example of the TextView class which is used to display text labels in Android apps is also a subclass of View. 

Or the other definition,

View refer to the android.view.View class, which is the base class of all UI classes. android.view.View class is the root of the UI class hierarchy. So from an object point of view, all UI objects are View objects. Following are some of the common View subclasses that will be used in android applications.

These are some of the view subclass available in android. 

ViewGroup

The ViewGroup class is a subclass of the View class. And also it will act as a base class for layouts and layouts parameters.  The ViewGroup will provide an invisible container to hold other Views or ViewGroups and to define the layout properties. For example, Linear Layout is the ViewGroup that contains UI controls like Button, TextView, etc., and other layouts also. ViewGroup Refer to the android.view.ViewGroup class, which is the base class of some special UI classes that can contain other View objects as children. Since ViewGroup objects are also View objects, multiple ViewGroup objects and View objects can be organized into an object tree to build a complex UI structure. Following are the commonly used ViewGroup subclasses used in android applications.

The ViewGroup subclasses listed above group View instances together and takes care of their layout. For instance, the LinearLayout will render the components after each other either horizontally or vertically.

Difference Table

                                          View                                                       ViewGroup
View is a simple rectangle box that responds to the user’s actions. ViewGroup is the invisible container. It holds View and ViewGroup
View is the SuperClass of All component like TextView, EditText, ListView, etc ViewGroup is a collection of Views[TextView, EditText, ListView, etc..], somewhat like a container.
A View object is a component of the user interface [UI] like a button or a text box, and it’s also called a widget. A ViewGroup object is a layout, that is, a container of other ViewGroup objects [layouts] and View objects [widgets]
Examples are EditText, Button, CheckBox, etc. For example, LinearLayout is the ViewGroup that contains Button[View], and other Layouts also.
View refers to the android.view.View class ViewGroup refers to the android.view.ViewGroup class
android.view.View which is the base class of all UI classes. ViewGroup is the base class for Layouts.

So, these all are the basic key difference between the View class and Viewgroup class in Android.

Article Tags :

Phan Lý Huỳnh @phanlyhuynh

Đã đăng vào thg 5 1, 2020 2:26 SA 8 phút đọc

Bài đăng này đã không được cập nhật trong 2 năm

Layout là nơi xác định cấu trúc cho giao diện người dùng trong ứng dụng của bạn, ví dụ như là Activity. Tất cả các thành phần trong Layouts được xây dựng bằng cách sử dụng phân cấp các đối tượng View và ViewGroup. View thường là các đối tượng mà người dùng có thể nhìn thấy và tương tác, còn Viewgroup là một container xác định cấu trúc layout cho View và các đối tượng ViewGroup khác

  • Các đối tượng View thường được gọi là Widget và có thể là một trong các subclasses như Button, TextView hoặc CheckedTextView,... Các đối tượng ViewGroup thường được gọi là layouts có thể là một trong các kiểu cấu trúc layout khác nhau như RelativeLayout hoặc ConstraintLayout,...

ConstraintLayout

  • ConstraintLayout cho phép bạn tạo layoutlớn và phức tạp với hệ thống phân cấp view không có view group lồng nhau. Nó tương tự như RelativeLayout ở chỗ tất cả các views được đặt theo mối quan hệ giữa các views, nhưng nó linh hoạt hơn RelativeLayout và dễ sử dụng hơn với trình chỉnh sửa layout của Android Studio

RelativeLayout

  • RelativeLayout cho phép sắp xếp các control theo vị trí tương đối giữa các control khác trên giao diện [kể cả control chứa nó].

LinearLayout

  • LinearLayout sẽ xếp các phần tử View con theo 2 dạng là dạng cột hoặc dạng hàng, không có view nào đè lên view nào.

TableLayout

  • Cho phép sắp các control theo dạng lưới [dòng và cột]. TableLayout sẽ xem dòng nào có số lượng control nhiều nhất để xác định rằng nó có bao nhiêu cột [lấy dòng có số lượng control nhiều nhất làm số cột chuẩn]

FrameLayout

  • Các view con trong FrameLayout là các view sẽ nằm chồng lên nhau, view thêm vào sau sẽ nằm đè lên view nằm phía dưới

AbsoluteLayout

  • Cho phép thiết lập các control giao diện theo vị trí tùy thích.

GridLayout

  • Sử dụng một dạng lưới các dòng mỏng và vô hạn để tách layout của nó thành: các hàng, các cột, và các ô [cell]

Làm việc với layout

Ở phần trên mình giới thiệu các dạng layout trong android. Ở trong phần này chúng ta cùng xem áp dụng một số dạng layout như thế nào

  • Linearlayout:
    Layout này sắp xếp các view con nằm trong nó theo một chiều ngang hoặc dọc thông qua thuộc tính orientation với giá trị là vertical[dọc] và horizontal[ngang].

Giao diện:

  • FrameLayout:
    Framlayout là loại layout đơn giản mà các view con bên trong nó sẽ được đặt chồng lên nhau, view sau sẽ chồng lên view trước và định vị với nhau bằng thuộc tính layout_gravity

Giao diện:

  • RelaytiveLayout:
    Đây là 1 viewgroup mà giúp cho bạn đặt view con ở bất kì vị trí nào mà mình thích, view sau sẽ phụ thuộc vào vị trí của những view trước đó.

Giao diện:

Widget

TextView

  • Được sử dụng để hiểnthị text.

EditText

  • Dùng để cho người dùng nhập vào text, ví dụ như: tên, email, địa chỉ,...
  • Đây là ô nhập liệu, có thể dùng để lấy dữ liệu, hoặc set dữ liệu lên nó bằng hàm setText[] và getText[];
  • Nó có 2 thuộc tính quan trọng là inputType: kiểu nhập vào, có thể để số, password,… và line: số dòng được hiển thị để nhập liệu

Button

  • Thực hiện một hành động khi người dùng nhấn nút.

CheckBox

  • Cho phép người dùng chọn hoặc bỏ chọn một options.
  • Đây là widget để lựa chọn đối tượng. nó có 2 phương thức quan trọng là
    • setChecked, dùng để thiết lập checked. Nếu ta setChecked[true] thì control đó được chọn, còn nếu set bằng false thì có nghĩa hủy đi control
    • isChecked, Phương thức này dùng để kiểm tra trạng thái được chọn hay không. Nếu được chọn thì trả về là true, còn không thì là false.
  • Checkbox cho phép ta lựa chọn nhiều đối tượng
  • Nó có sự kiện onCheckedChangeListener để xử lý sự kiện khi thay đổi checked của một item

RadioButton

  • Cho phép người dùng chọn một lựa chọn từ một nhóm Radio định trước.
  • Đây là 1 widget, tương tự như checkbox, tuy nhiên thì nó không thể lựa chọn nhiều hơn 2 item cùng một lúc.
  • Nó được đặt trong Radio Group.
  • Ta có thể xử lý sự kiện khi chọn, thay đổi các item dựa vào các id của radio button trong radio group.

Spinner

  • Cho phép người dùng chọn một lựa chọn từ một danh sách.
  • Nó là một danh sách xổ xuống các item mà từ đó ta có thể lựa chọn được 1 item ở trong danh sách đó.
  • Danh sách data sẽ được đổ vào một adapter[adapter này có thể là array adapter do android studio cung cấp sẵn hoặc mình có thể custom]. Sau đó adapter này sẽ được đổ vào spinner
  • Các phương thức đặc chưng của spinner:
    • setAdapter[Adapter]: set adapter cho spinner[có sẵn hoặc custom]
    • setSelection[index] : lực chọn item ở vị trí index trong spinner
    • getSelectedItemPositon[]: trả về vị trí position của item đang được chọn, nếu chưa chọn trả về -1
    • getSelectedItem[]: trả về đối tượng đang được chọn

ProgressBar

  • ProgressBar để biểu thị quá trình của hành động.

SeekBar

  • Cho phép người dùng chọn một giá trị bằng việc kéo thả con trượt qua trái hoặc phải
  • Seekbar là một lớp mở rộng từ progessbar, nó có thêm một cần gạt. Người dùng có thể chạm vào cần gạt để kéo sang trái hoặc phải để thiết lập giá trị của tiến trình[ progess].
  • Các thuộc tính đặc biệt:
    • Id: dùng để định danh seekbar
    • Max: thuộc tính dùng để định nghĩa giá trị tối đa của seekbar
    • Progess: xác định giá trị mặc định ban đầu của progess
    • secondProgess: xác định giá trị ban đầu của tiến trình phụ
    • thumb: đặt icon cho seekbar

RatingBar

  • Cho phép người dùng đánh giá một thứ gì đó bằng việc chọn một hoặc nhiều sao.

ImageView

  • Hiển thị một hình ảnh.
  • Có các loại chất lượng cho hình ảnh sau:
    • Ldpi: low-120dpi
    • mdpi [medium] ~160dpi
    • hdpi [high] ~240dpi
    • xhdpi [extra-high] ~320dpi
    • xxhdpi [extra-extra-high] ~480dpi
    • xxxhdpi [extra-extra-extra-high] ~640dpi
  • Android hỗ trợ các loại ảnh như PNG,JPG,GIF.

ImageButton

  • Làm việc như một nút nhưng hiển thị một hình ảnh thay vì văn bản.

DatePicker

  • Cho phép người dùng lựa chọn ngày.

TimePicker

  • Cho phép người dùng lựa chọn thời gian

CalendarView

  • Cho phép người dùng chọn lịch

ScrollView

  • Tự động hiển thị thanh cuộn dọc nếu giao diện không vừa màn hình hiện tại.
  • ScrollView là một dạng đặc biệt của FrameLayout ở chỗ nó cho phép người dùng di chuyển qua một danh sách các quan điểm mà chiếm nhiều không gian hơn màn hình vật lý. Các ScrollView có thể chứa chỉ một Child View hay ViewGroup, mà thường là một LinearLayout.
  • Nó có thuộc tính scrollbar với giá trị là vertical[chiều dọc] hoặc horizontal[chiều ngang]


All rights reserved

Video liên quan

Chủ Đề