android開發系列教程(一)啟動頁
阿新 • • 發佈:2018-12-15
1.啟動頁延時跳轉+沉浸狀態列+啟動頁圖片全屏
public class SplashActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*沉浸狀態列,即使狀態列顏色透明*/ /* View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); getWindow().setStatusBarColor(Color.TRANSPARENT);*/ /**************************************/ //去除標題欄 // requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉Activity上面的狀態列 getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN); /**************************************/ setContentView(R.layout.activity_splash); //延時2秒跳轉MainActivity new Handler().postDelayed(new Runnable() { public void run() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intent); } }, 2000); //2秒 } }
activity_splash.xml: (主要程式碼是android:scaleType="fitXY",x軸,y軸分別拉伸圖片)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/splash_bg_img" android:src="@mipmap/splash" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" /> </RelativeLayout>
2.解決啟動白屏問題:
再values/color中新增#90FFFFFF透明色
<color name="transparent">#90FFFFFF</color>
在style中新增主題
</style> <style name="AppTransparentTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
在Androidmainfest.xml設定:
<activity android:name=".SplashActivity" android:theme="@style/AppTransparentTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>