Android進階2之APK方式換膚

來源:互聯網
上載者:User

public class MainActivity extends Activity{private Button defaultbutton = null;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);//預設皮膚defaultbutton = (Button)findViewById(R.id.defaultButton);defaultbutton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){layout.setBackgroundResource(R.drawable.netskin);}});//為其他皮膚添加點擊按鈕ArrayList<PackageInfo> skinList = getAllSkin();for (int i = 0; i < skinList.size(); i++){try{final Context context = createPackageContext(skinList.get(i).packageName,Context.CONTEXT_IGNORE_SECURITY);Button btn = new Button(this);btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));btn.setText(context.getText(R.string.app_name));btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.netskin));}});layout.addView(btn);}catch (NameNotFoundException e){e.printStackTrace();}}}/** * 擷取所有已安裝的皮膚主題 *  * @return */private ArrayList<PackageInfo> getAllSkin(){ArrayList<PackageInfo> skinList = new ArrayList<PackageInfo>();List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);for (PackageInfo p : packs){if (isSkinPackage(p.packageName)){skinList.add(p);}}return skinList;}/** * 判斷是否是皮膚主題 *  * @param packageName * @return */private boolean isSkinPackage(String packageName){// 自己製作的皮膚主題包名 例如:sunlight.skin0 sunlight.skin1等等String rex = "sunlight.skin\\w";Pattern pattern = Pattern.compile(rex);Matcher matcher = pattern.matcher(packageName);return matcher.find();}}

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/defaultButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="預設皮膚" /></LinearLayout>

原始碼下載:點擊開啟連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.