android 如何依赖android:sharedUserId更换皮肤Skin
16lz
2021-12-04
为了更好的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(安卓)中 java 与 webview 的交互
- 如果让我重新设计一款Android(安卓)App
- 在Android中自定义IOS风格的按钮
- 第一章 开始启程,你的第一行Android代码
- 哪里可以下载Android的源代码
- 如果你想用对话框代替一个activity的话,可以设置activity的主题属
- android studio使用快捷键大全
- 关于Android(安卓)studio 使用fastjson报错的解决方法
- [Android] 获取Android设备的唯一识别码|设备号|序号|UUID