在编写android应用程序的过程中,经常会使用到导航控件。如果每一个activity都需要用到导航控件,可以将这个导航控件独立出来,方便程序开发。

   写一个导航的代码类:

  

public class NavigationBar extends RelativeLayout implements OnClickListener {    public static final int NAVIGATION_BUTTON_LEFT = 0;    public static final int NAVIGATION_BUTTON_RIGHT = 1;    private Context mContext;    private NavigationBarListener mListener;    public NavigationBar(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(context);    }    public NavigationBar(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public NavigationBar(Context context) {        super(context);        init(context);    }    private void init(Context context) {        mContext = context;        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-1, -2);        this.setLayoutParams(lp);        this.setBackgroundResource(R.drawable.navigation_bar_bg);    }    public void setLeftBarButton(String title) {        setButton(title, NAVIGATION_BUTTON_LEFT);    }    public void setRightBarButton(String title) {        setButton(title, NAVIGATION_BUTTON_RIGHT);    }    private void setButton(String title, int which) {        // remove the old button (if there is one)        Button oldButton = (Button) this.findViewWithTag(new Integer(which));        if (oldButton != null)            this.removeView(oldButton);        Button newButton = new Button(mContext);        newButton.setTag(new Integer(which)); // used to determine which button is pressed and to remove old buttons        // set OnClickListener        newButton.setOnClickListener(this);        // set LayoutParams        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-2, -2);        if (which == NAVIGATION_BUTTON_LEFT)            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);        else if (which == NAVIGATION_BUTTON_RIGHT)            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);        else            throw new IllegalArgumentException("Parameter 'which' must be 0 or 1");        lp.addRule(RelativeLayout.CENTER_VERTICAL);        lp.setMargins(10, 0, 10, 0);        newButton.setLayoutParams(lp);        // set button text        newButton.setText(title);        newButton.setTextSize(12);        newButton.setTextColor(Color.WHITE);        // set button drawable        newButton.setBackgroundResource(R.drawable.navigation_bar_btn);        // add button        this.addView(newButton);    }    public void setBarTitle(String title) {        // remove old title (if exists)        TextView oldTitle = (TextView) this.findViewWithTag("title");        if (oldTitle != null)            this.removeView(oldTitle);        TextView newTitle = new TextView(mContext);        newTitle.setTag("title");        // set LayoutParams        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-2, -2);        lp.addRule(RelativeLayout.CENTER_IN_PARENT);        lp.setMargins(0, 30, 0, 30);        newTitle.setLayoutParams(lp);        // set text        newTitle.setText(title);        newTitle.setTextSize(22);        newTitle.setTextColor(Color.WHITE);        // add title to NavigationBar        this.addView(newTitle);    }    public void setNavigationBarListener(NavigationBarListener listener) {        mListener = listener;    }    @Override    public void onClick(View v) {        int which = ((Integer) v.getTag()).intValue();        if (mListener != null) {            mListener.OnNavigationButtonClick(which);        }    }    /**     * Listener for NavigationBar.     */    public interface NavigationBarListener {        /**         * Called when the user presses either of the buttons on the NavigationBar.         *         * @param which - indicates which button was pressed, ie: NavigationBar.NAVIGATION_BUTTON_LEFT         */        public void OnNavigationButtonClick(int which);    }}

 背景样式定义navigation_bar_bg.xml:

  

<?xml version="1.0" encoding="utf-8"?>                                      

 导航按钮样式定义navigation_bar_btn.xml

<?xml version="1.0" encoding="utf-8"?>                                                                                                                                                                        

 那么在activity怎么使用呢?

activity调用代码如下:

  

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.change_device);NavigationBar nb = (NavigationBar)findViewById(R.id.detailNavBar);        nb.setLeftBarButton(getString(R.string.cancel));        nb.setRightBarButton(getString(R.string.save));        nb.setBarTitle(getString(R.string.details));        NavigationBar.NavigationBarListener nbl = new NavigationBar.NavigationBarListener() {            @Override            public void OnNavigationButtonClick(int which) {                if (which == NavigationBar.NAVIGATION_BUTTON_LEFT) {                    finish();                } else {                                }            }        };        nb.setNavigationBarListener(nbl);}

 在layout里面定义我们的导航控件

  

<?xml version="1.0" encoding="utf-8"?>                        

 现在看看效果图:

那么在你以后想使用这个控件的时候,就可以直接调用了。

更多相关文章

  1. android 文本框实现搜索和清空效果
  2. android EditText使用指南
  3. Android(安卓)事件触发机制
  4. android 触发AppWidget上控件事件来更新AppWidget
  5. Android入门:基本控件(一)
  6. Android定义的路径全局变量
  7. android RatingBar自定义图片
  8. android布局 LinearLayout和RelativeLayout
  9. Android中PopupWindow自定义坐标实现

随机推荐

  1. Android(安卓)Okhttp + Android提交post
  2. Eclipse And Android 使用心得
  3. Android ApiDemos示例解析(52):Graphics-
  4. Android应用程序组成
  5. Android课堂练习——图文显示
  6. 解析ClassLoader
  7. Android ActionBar完全解析,使用官方推荐
  8. Android之——Handler随笔
  9. android开发 drawable中XML的相关应用
  10. Gallery图像重叠问题以及每屏只显示一张