一、顯示壁紙
顯示壁紙也是launcher必不可少的功能,下面我們看看如何讓我們開發的launcher來顯示壁紙。
要在我們的activity裏顯示一個壁紙非常簡單(包括動態壁紙也如此),我們只需要定義一個theme使其繼承自android:Theme.Wallpaper,然後在activity中使用這個theme就ok了。
在res/valuse下面增加一個xml文件,其名稱爲styles.xml(AndroidStudio新建的項目會自動創建styles.xml我們只需要在resources標記對下添加),內容如下:
<style name="SAOTheme" parent="android:Theme.Wallpaper">
<!-- windowNoTitle設置爲true,去掉標題欄 -->
<item name="android:windowNoTitle">true</item>
</style>
此時整個工程的結果如下:
下面在AndroidManifest.xml中使用這個theme,如下圖所示:
xml代碼如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sljjyy.sao.launcher"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sljjyy.sao.launcher.MainActivity"
android:theme="@style/SAOTheme"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
好了,運行程序,可以看到壁紙的顯示效果了:
二、設置壁紙
用代碼設置壁紙也是非常地簡單的事,我們只需要向系統發送一個“設置請求”就足夠了,其它的事情繫統處理。
用下面代碼表示:
public void onSetWallpaper(View view) {
//生成一個設置壁紙的請求
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,"chooser_wallpaper");
//發送設置壁紙的請求
startActivity(chooser);
}
原教程中是在新建項目中加Button實現的,但是由於審美的關係,不希望加個很醜的Button在界面上,然後就發現了AndroidStudio已經貼心的預設了Menu,不過在搞了很久才搞定的
res/menu/main.xml 直接可以使用
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings"
android:title="settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
Activity上也已經做好了關聯
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
我們只需要設定好動作 調用onSetWallpaper()方法就可以了
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.action_settings:
onSetWallpaper();
return true;
}
return false;
}
android手把手教你開發launcher(一)(AndroidStudio版)
android手把手教你開發launcher(二)——列出安裝的應用程序
android手把手教你開發launcher(三)——啓動安裝的應用程序
android手把手教你開發launcher(四)——顯示widget
android手把手教你開發launcher(五)——設置壁紙