Android爱读app开发记录之三---皮肤更换

          皮肤更换的话,要做好还得配置一些theme什么的,感觉挺麻烦的,我这里就单纯的换一下背景并用SharedPreferences保存用户使用记录就行了

          我的思路是:用户点击菜单项→弹出一个对话框→用户点击相应的Button→给gridView设置背景→用SharedPreferences保存设置

一、弹出对话框


相关代码

case R.id.item04:
			AlertDialog dialog = new AlertDialog.Builder(this).create();
			dialog.show();
			dialog.setContentView(R.layout.mydialog);

mydialog.xml文件

很简单就是一个居中的线性布局嵌套了几个Button

<?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"
   >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择合适的背景"
        android:textSize="28dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_margin="5dp"
        android:gravity="center_horizontal">
    <Button
        android:id="@+id/shelfBg0"
        android:text="@string/bg_white"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_marginRight="5dp"
        android:background="@color/shelf_background0"/>
     <Button
        android:id="@+id/shelfBg1"
        android:layout_width="50dp"
         android:text="@string/bg_pink"
        android:layout_height="30dp"
        android:layout_marginRight="5dp"
        android:background="@color/shelf_background1"/>
      <Button
        android:id="@+id/shelfBg2"
        android:layout_width="50dp"
          android:text="@string/bg_yellow"
        android:layout_height="30dp"
        android:layout_marginRight="5dp"
        android:background="@color/shelf_background2"/>
       <Button
        android:id="@+id/shelfBg3"
        android:layout_width="50dp"
           android:text="@string/bg_simulation"
        android:layout_marginRight="5dp"
        android:layout_height="30dp"
        android:background="@color/shelf_background3"/>
       </LinearLayout>
    </LinearLayout>
 </LinearLayout>
显示的效果如下


二、对话框监听事件及保存

        

dialog.findViewById(R.id.shelfBg0).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					toolbarGrid.setBackground(mImages[0]);
					editor = sp.edit();
					editor.putInt("mSkins",mImages[0]);
					editor.commit();
					adapter.notifyDataSetChanged();
				}
			});
这里只举出了第一个Button的监听事件,其他的也一样就不贴代码了

最后写一个方法在每次重新启动(onCreate)的时候调用就OK了

代码如下

//初始化皮肤
	public void iniSkin(){
		toolbarGrid.setBackground(sp.getInt("mSkins",mImages[0]));
	}

三、最终效果图

设置皮肤



下次进入



全文完!感谢阅读!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值