Android 反射实战 - 更换APP皮肤<2>

来源:互联网 发布:spss22 mac版教程 编辑:程序博客网 时间:2024/06/15 20:49

前面通过实现Factory来获取所有的UI和属性等.将前面那个工程进行调整,参考其他网络上面的一些信息,调整和更新ActionBar右上角的menu字体颜色和背景,通过这个小例子,从某种程度可以反映更换APP皮肤基本的思路.


package com.oneplus.factoryimplapp;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.util.AttributeSet;import android.view.InflateException;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.widget.TextView;import com.oneplus.factoryimplapp.R;import com.oneplus.base.OneplusBaseActivity;/** *  *//** * @author zhibao.liu * @date 2015-12-8 * @company : oneplus.Inc */public class OneplusFactoryActivity extends /*OneplusBase*/Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.oneplus_factory);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.oneplus_factory, menu);        setMenuBackground();        return true;    }    /** set menu style */    protected void setMenuBackground() {        this.getLayoutInflater().setFactory(                new android.view.LayoutInflater.Factory() {                    public View onCreateView(String name, Context context,AttributeSet attrs) {                        // set self-define inflate                        if (name.equalsIgnoreCase("TextView")) {                            try {                                LayoutInflater f = getLayoutInflater();                                final View view = f.createView(name, null,attrs);                                new Handler().post(new Runnable() {                                    public void run() {                                        // set background color                                        view.setBackgroundResource(R.color.oneplus_menu);                                        // set font color                                        ((TextView) view).setTextColor(0xff0000ff);                                    }                                });                                return view;                            } catch (InflateException e) {                                e.printStackTrace();                            } catch (ClassNotFoundException e) {                                e.printStackTrace();                            }                        }                        return null;                        }                }            );    }}

这是一个非常简单的更新style的例子.






0 0
原创粉丝点击