如何使用Android順序佈局?


在學習Android順序佈局之前,我們應該瞭解Android中的順序佈局是什麼。順序佈局包含一系列步驟和進度條。根據順序,它會顯示一個動畫進度條。

本例演示瞭如何使用Android順序佈局。

步驟1 − 在Android Studio中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟2 − 開啟build.gradle(app)並新增設計支援庫依賴項。

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 19
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.google.code.gson:gson:2.8.5'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   implementation 'com.github.transferwise:sequence-layout:1.0.7'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

步驟3 − 開啟build.gradle(project)並新增設計支援庫依賴項。

// 頂層構建檔案,您可以在其中新增所有子專案/模組共有的配置選項。

buildscript {
   repositories {
      google()
      jcenter()
   }
   dependencies {
      classpath 'com.android.tools.build:gradle:3.2.1'
      // NOTE: Do not place your application dependencies here; they belong
      // in the individual module build.gradle files
   }
}
allprojects {
   repositories {
      google()
      jcenter()
      maven { url "https://jitpack.io" }
   }
}
task clean(type: Delete) {
   delete rootProject.buildDir
}

步驟4 − 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<com.transferwise.sequencelayout.SequenceLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">
   <com.transferwise.sequencelayout.SequenceStep
      android:id="@+id/first"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:subtitle="Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
         Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
      app:anchor="30 Nov"
      app:title="First step"/>
   <com.transferwise.sequencelayout.SequenceStep
      android:id="@+id/second"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:subtitle="Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
         Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
      app:title="Second step"/>
   <com.transferwise.sequencelayout.SequenceStep
      android:id="@+id/third"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:anchor="Today"
      app:title="Third step"
      app:subtitle="Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
         Lorem Ipsum has been the industry's standard dummy text ever since the 1500s" />
</com.transferwise.sequencelayout.SequenceLayout>

在上面,我們將順序佈局宣告為父佈局,並將順序步驟新增為各個步驟。每個步驟都包含錨點檢視、標題和副標題。

步驟5 − 將以下程式碼新增到src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.transferwise.sequencelayout.SequenceStep;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   SequenceStep sequenceStep,sequenceStep2,sequenceStep3;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      sequenceStep=findViewById(R.id.first);
      sequenceStep2=findViewById(R.id.second);
      sequenceStep3=findViewById(R.id.third);
      sequenceStep2.setActive(true);
      sequenceStep.setOnClickListener(this);
      sequenceStep2.setOnClickListener(this);
      sequenceStep3.setOnClickListener(this);
   }
   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.first:
            Toast.makeText(MainActivity.this,"This is first step",Toast.LENGTH_LONG).show();
            break;
         case R.id.second:
            Toast.makeText(MainActivity.this,"This is second step",Toast.LENGTH_LONG).show();
            break;
         case R.id.third:
            Toast.makeText(MainActivity.this,"This is Third step",Toast.LENGTH_LONG).show();
            break;
      }
   }
}

在上面的程式碼中,我們聲明瞭順序步驟並給出了點選監聽器。要啟用步驟,請使用以下程式碼。

sequenceStep2.setActive(true);

在上面的程式碼中,我們聲明瞭sequence2處於活動狀態,這意味著進度條將顯示到步驟2。

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要在Android Studio中執行應用程式,請開啟專案的活動檔案之一,然後單擊執行 工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 −

在上面的示例中,由於我們聲明瞭活動模式,因此它顯示了進度條。

更新於:2019年7月30日

432 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.