android实现第一次打开应用的介绍页面
效果图
添加依赖
implementation 'com.github.AppIntro:AppIntro:6.2.0'
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() maven { url "https://jitpack.io" } mavenCentral() jcenter() // Warning: this repository is going to shut down soon } } rootProject.name = "onbor" include ':app'
文件中加入上面加粗内容
IntroActivity文件内容
import android.content.Intent; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.fragment.app.Fragment; import com.github.appintro.AppIntro; import com.github.appintro.AppIntroPageTransformerType; public class IntroActivity extends AppIntro { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setProgressIndicator(); setNextArrowColor(Color.parseColor("#2196F3")); setColorDoneText(Color.parseColor("#2196F3")); setSkipButtonEnabled(false); setTransformer(AppIntroPageTransformerType.Fade.INSTANCE); addSlide(new PermissionActivity()); addSlide(new WelcomeActivity()); } @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onDonePressed(Fragment currentFragment) { super.onDonePressed(currentFragment); finish(); Intent intent = new Intent(IntroActivity.this, MainActivity.class); startActivity(intent); } }
addSlide(new WelcomeActivity());
这个class写起来和fragment一样,正常xml就可以
import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.fragment.app.Fragment; public class PermissionActivity extends Fragment { private View root; @RequiresApi(api = Build.VERSION_CODES.M) @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (root == null) { root = inflater.inflate(R.layout.activity_permission, container, false); } return root; } }
manifest的声明给父亲就可以子class不要
<activity android:name=".IntroActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
GitHub地址 下载前给star