一、显示壁纸
显示壁纸也是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(五)——设置壁纸