如何使用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.