Android開發學習筆記(十)實現一個簡易的開場介面

來源:互聯網
上載者:User

很多應用都有按標之後首先顯示一個歡迎介面,再跳轉到主activity的功能,現在來實現以下。我主要參考了CSDN上的一個仿weichat開場介面的部落格,相當於一個簡易版本吧。

方法一,利用系統提供的函數。

首先要新建立一個class,名為Welcome.java,主activity為MainActivity.java。

這裡利用了兩個函數用來把R.layout.start的內容全螢幕顯示。

1)getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //消除狀態列

注意:

public void addFlags(int flags) {          setFlags(flags, flags);  }  

所以這個函數也可以寫成:getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

2)requestWindowFeature(Window.FEATURE_NO_TITLE); //消除標題列

這個函數要寫在setContentView之前,否則無效。

開場畫面活動,Welcome.java

package com.xujin.smarttaskman;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.Window;import android.view.WindowManager;/** 開場歡迎動畫 */public class Welcome extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//全螢幕顯示welcome畫面requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.start);//延遲0.7秒後執行run方法中的頁面跳轉new Handler().postDelayed(new Runnable() {@Overridepublic void run() {Intent intent = new Intent(Welcome.this, MainActivity.class);startActivity(intent);Welcome.this.finish();}}, 700);}}

然後是最基本的xml檔案

start.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/welcome"    android:background="#ffffff"    android:scaleType="centerCrop"/></LinearLayout>

注意,這裡的scaleType一定要centerCrop(使圖片覆蓋整個ImageView),否則顯示效果很糟糕。

以上這種方法可能出現瞬間出現非全螢幕模式的錯誤。

方法二,更改AndroidMainfest.xml的內容。

設定Welcome為點開按鈕後首先顯示的活動,them要設定成無標題列全屏。

<activity            android:name="com.xujin.smarttaskman.Welcome"            android:label="@string/app_name"             android:theme="@android:style/Theme.NoTitleBar.Fullscreen">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

這種方法沒有顯式使用函數,簡單易懂,且不出現第一種方法瞬間出現非全螢幕模式的錯誤。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.