android 如何依赖android:sharedUserId更换皮肤Skin

 为了更好的UI体验,不错的App都会有几套不错的SKIN提供给用户来选择,以下是两个比较简单的工程,一个为主工程.apk,一个为皮肤.apk:

先看皮肤:

AndroidMainfest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tal.skin"
      android:versionCode="1"
      android:versionName="1.0"
      android:sharedUserId="com.tal.skinmain">①

    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Skin"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

   注释:① android:sharedUserId = “com.tal.skinmain” 通过Shared User id,拥有同一个User id的多个APK可以配置成运行在同一个进程中.所以默认就是可以互相访问任意数据. 也可以配置成运行成不同的进程, 同时可以访问其他APK的数据目录下的数据库和文件.就像访问本程序的数据一样.

然后再皮肤工程里面放置两张图片,来显示替换皮肤样式。skin1.png,skin2.png

皮肤工程无需activity界面即可;

主工程:

AndroidMainfest.xml按照正常写法即可,无需配置什么东东,当时在设置皮肤的需要这样写上:

Activity.java

 package com.tal.skinmain;

import com.tal.skin.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 *
 * @author Tal
 * @time  2011-5-9下午09:32:47
 * @Email dangzhengtao@gmail.com
 * @tag   可以更换皮肤的主工程
 */
public class SkinMain extends Activity {
 
 LinearLayout  linearLayout;
 TextView textview;
 Context ctx;
 Button bt1,bt2,bt3;
 boolean flag = true;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        textview = (TextView)findViewById(R.id.id_skin_textview);
        textview.setText("皮肤一");
        linearLayout = (LinearLayout)findViewById(R.id.id_skin_linearlayout);
       
        linearLayout.setBackgroundColor(Color.BLUE);
       
        try {
         //获取皮肤共享的数据包
         ctx = this.createPackageContext("com.tal.skin", CONTEXT_IGNORE_SECURITY);②
        } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
  }
   
  bt1 = (Button)findViewById(R.id.id_skin_bt1);
  bt2 = (Button)findViewById(R.id.id_skin_bt2);
  bt3 = (Button)findViewById(R.id.id_skin_bt3);
  bt1.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    linearLayout.setBackgroundColor(Color.BLUE);
    textview.setText("默认皮肤");
   }});
  bt2.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    linearLayout.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.skin1));  ③
    textview.setText("皮肤一");
   }});
  bt3.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
       linearLayout.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.skin2));④
    textview.setText("皮肤二");
   }});
    }
}

    注解:②③④获取皮肤工程的数据,当中有指定皮肤的包名

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/id_skin_linearlayout"
    >
<TextView
    android:id="@+id/id_skin_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
 <Button
   android:id="@+id/id_skin_bt1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="默认"  
   />
    <Button
   android:id="@+id/id_skin_bt2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="皮肤一"  
   />
    <Button
   android:id="@+id/id_skin_bt3"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="皮肤二"  
   />
</LinearLayout>
   OK。

运行如图:

android 如何依赖android:sharedUserId更换皮肤Skin - 爱海涛涛 - Google Android
android 如何依赖android:sharedUserId更换皮肤Skin - 爱海涛涛 - Google Android
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值