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

前面通过实现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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值