android 自定义 背景

 

package com.huawei.st;

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.MenuInflater;
import android.view.View;

public class Test1 extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  MenuInflater menuInflater = getMenuInflater();
  menuInflater.inflate(R.menu.home_timeline_menu, menu);
  
  setMenuBackground();
  return true;
 }

 protected void setMenuBackground() {
  Test1.this.getLayoutInflater().setFactory(
    new android.view.LayoutInflater.Factory() {
     /**
      * name - Tag name to be inflated.<br/>
      * context - The context the view is being created in.<br/>
      * attrs - Inflation attributes as specified in XML file.<br/>
      */
     public View onCreateView(String name, Context context,
       AttributeSet attrs) {
      // 指定自定义inflate的对象
      if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
       try {
        LayoutInflater f = getLayoutInflater();
        final View view = f.createView(name, null,
          attrs);
        new Handler().post(new Runnable() {
         public void run() {
          // 设置背景图片
          view.setBackgroundResource(R.drawable.bg_state);
         }
        });
        return view;
       } catch (InflateException e) {
        e.printStackTrace();
       } catch (ClassNotFoundException e) {
        e.printStackTrace();
       }
      }
      return null;
     }
    });
 }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值