Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách sử dụng View Stub trong Android

Trước khi đi vào ví dụ, chúng ta nên biết View Stub là gì trong Android. Đây là một view lazy inflate có kích thước bằng 0. Nó sẽ inflate tại runtime. Sử dụng phương thức inflate(), nó sẽ inflate tại runtime và thêm vào Window Manager hoặc View Group. Sử dụng setVisibility(int), chúng ta có thể show/hide View Stub trong Android.

Ví dụ này minh họa cách sử dụng View Stub trong Android.

Bước 1

Tạo một dự án mới trong Android Studio, vào File → New Project và điền đầy đủ thông tin cần thiết để tạo dự án mới.

Bước 2

Thêm mã sau vào res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:id="@+id/layout"    android:layout_height="match_parent"    android:orientation="vertical">    <Button       android:id="@+id/show"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="show"/>    <Button       android:id="@+id/hide"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="Hide"/>    <ViewStub       android:id="@+id/viewStub"       android:layout_width="match_parent"       android:layout="@layout/childlayout"       android:layout_height="300dip" >    </ViewStub> </LinearLayout>

Trong mã trên, chúng ta tạo 2 button show và hide. Nó sẽ show/hide View Stub layout theo yêu cầu. Tiếp theo, chúng ta khai báo View Stub và inflate layout như dưới:

android:layout="@layout/childlayout"

Trong mã trên, chúng ta inflate child layout vào View Stub. Khi khai báo child layout cho View Stub, nó không inflate tự động. Chúng ta phải gọi phương thức inflate() trong Activity class.

Bước 3

Thêm mã sau vào src/MainActivity.java

package com.example.andy.myapplication; import android.annotation.TargetApi; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewStub; import android.widget.Button; public class MainActivity extends AppCompatActivity {    @TargetApi(Build.VERSION_CODES.O)    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       final ViewStub viewStub = findViewById(R.id.viewStub);       viewStub.inflate();       Button show = findViewById(R.id.show);       show.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {             viewStub.setVisibility(View.VISIBLE);          }       });       Button hide = findViewById(R.id.hide);       hide.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {             viewStub.setVisibility(View.GONE);          }       }); } }

Trong mã trên, chúng ta khai báo View Stub và inflate View Stub như dưới:

final ViewStub viewStub = findViewById(R.id.viewStub); viewStub.inflate();

Click vào button Show, nó sẽ hiển thị View Stub. Click vào button Hide, nó sẽ ẩn View Stub.

Bước 4

Không cần thay đổi manifest.xml.

Hãy thử chạy ứng dụng. Giả sử bạn đã kết nối thiết bị Android thực tế với máy tính. Để chạy ứng dụng từ Android Studio, mở một file activity của dự án và nhấn biểu tượng Run Cách sử dụng View Stub trong Android từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn và kiểm tra thiết bị sẽ hiển thị màn hình mặc định.

Cách sử dụng View Stub trong Android

Ban đầu, nó hiển thị như trên. Khi click Hide button nó sẽ biến mất như hình dưới:

Cách sử dụng View Stub trong Android

Bây giờ click vào Show button, nó sẽ hiển thị ImageView như hình dưới:

Cách sử dụng View Stub trong Android