【发布时间】:2014-07-13 20:49:12
【问题描述】:
我制作了自己的壁纸,但当用户单击“设置壁纸”按钮时,我不知道如何设置它
我写道:
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
但不是代码,我需要你的帮助。
【问题讨论】:
我制作了自己的壁纸,但当用户单击“设置壁纸”按钮时,我不知道如何设置它
我写道:
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
但不是代码,我需要你的帮助。
【问题讨论】:
这是我的解决方案 .... 使用 Intent 启动 MyWallPaperService
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(getApplicationContext(), MyWallpaperService.class));
startActivity(intent);
谢谢!
【讨论】:
你可以改变一个Activity的背景:
在Activity 布局中,将根View(通常为LinearLayour 或RelativeLayout)设置为android:background="@drawable/yourdrawable"
以编程方式:
LinearLayout ll = (LinearLayout) findViewById(R.id.myLinearLayout);
ll.setBackgroundDrawable(...)
或为您的应用添加主题或Activity。
在您的情况下,您有兴趣以编程方式更改它,因此您需要使用第二个选项。您需要在按钮中添加onClickListener 并在里面更改背景,如下所示:
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
LinearLayout ll = (LinearLayout) findViewById(R.id.myLinearLayout);
ll.setBackgroundDrawable(...)
}
});
顺便说一句,要更改您的活动背景,您不需要使用权限
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
你有类似的问题:
How to programmatically change the background image of an Android Activity
Change Layout-background on click programmatically
Android: Changing Background-Color of the Activity (Main View)
How to set background color of an Activity to white programmatically?
【讨论】: