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

Cách sử dụng TabHost trên Android

Trước khi đi vào ví dụ, chúng ta nên biết TabHost là gì trong Android. TabHost chứa một tập hợp các tab. Mỗi tab chứa fragment hoặc activity tùy theo đặc thảo dự án. Người dùng có thể cuộn tab từ trái sang phải hoặc ngược lại.

Ví dụ này minh họa cách sử dụng TabHost 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"    xmlns:app="https://schemas.android.com/apk/res-auto"    xmlns:tools="https://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <TabHost android:id="@+id/tabhost"       android:layout_width="match_parent"       android:layout_height="match_parent" >       <LinearLayout          android:orientation="vertical"          android:layout_width="fill_parent"          android:layout_height="fill_parent">          <TabWidget android:id="@android:id/tabs"             android:layout_width="fill_parent"             android:layout_height="wrap_content" />          <FrameLayout android:id="@android:id/tabcontent"             android:layout_width="fill_parent"             android:layout_height="fill_parent">             <LinearLayout                android:id="@+id/tab1"                android:layout_width="match_parent"                android:layout_height="match_parent">                <Button                   android:id="@+id/button"                   android:layout_width="wrap_content"                   android:layout_height="wrap_content"                   android:text="tab1" />             </LinearLayout>             <LinearLayout                android:id="@+id/tab2"                android:layout_width="match_parent"                android:layout_height="match_parent">                <Button                   android:id="@+id/button2"                   android:layout_width="wrap_content"                   android:layout_height="wrap_content"                   android:text="tab2" />             </LinearLayout>          </FrameLayout>       </LinearLayout> </TabHost> </LinearLayout>

Trong layout trên, chúng ta khai báo FrameLayout làm con của TabWidget (Theo android.com, TabWidget yêu cầu FrameLayout làm nội dung).

Bước 3

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

import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.RadioButton; import android.widget.TabHost; public class MainActivity extends AppCompatActivity {    RadioButton radioButton;    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       TabHost tabs = (TabHost) findViewById(R.id.tabhost);       tabs.setup();       TabHost.TabSpec spec = tabs.newTabSpec("tag1");       spec.setContent(R.id.tab1);       spec.setIndicator("First");       tabs.addTab(spec);       spec = tabs.newTabSpec("tag2");       spec.setContent(R.id.tab2);       spec.setIndicator("second");       tabs.addTab(spec);    } }

Bước 4

Không cần thay đổi file 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 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 TabHost trên Android

Bây giờ nhấn vào tab thứ hai. Nó sẽ cho kết quả như hình dưới:

Cách sử dụng TabHost trên Android