Android custom Main Menu

Source: Internet
Author: User

Android custom Main Menu

This article introduces a simple custom Main Menu with the following effects:

Principle: it is actually a simple encapsulation of the native Dialog. In addition, the animation effects are displayed and hidden. Add a callback event to the control.

 

TestDialog. java

 

Public class TestDialog implements OnClickListener {private Context mContext; private Dialog mDialog; private Display mDisplay; private TextView mTitle; private TextView mOk, mCancel; private LinearLayout mContent; private List
 
  
MItems = null; private List
  
   
MListeners = null; private List
   
    
MDrawables = null; public TestDialog (Context context) {this. mContext = context; WindowManager windowManager = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); mDisplay = windowManager. getdefadisplay display ();}/*** builder () Create ** @ return */public TestDialog builder (int layout) {initView (layout); return this ;} /*** Method for initializing Dialog View ** @ param layout */private void initView (int layout) {View View = LayoutInflater. from (mContext ). inflate (layout, null); view. setMinimumWidth (mDisplay. getWidth (); // mTitle = (TextView) view. findViewById (R. id. menu_title); mContent = (LinearLayout) view. findViewById (R. id. menu_content); mOk = (TextView) view. findViewById (R. id. menu_ OK); mCancel = (TextView) view. findViewById (R. id. menu_cancel); mOk. setOnClickListener (this); mCancel. setOnClickListener (this); mDialog = n Ew Dialog (mContext, R. style. dialogStyle); mDialog. setContentView (view); Window dialogWindow = mDialog. getWindow (); dialogWindow. setGravity (Gravity. LEFT | Gravity. BOTTOM); WindowManager. layoutParams lp = dialogWindow. getAttributes (); lp. x = 0; lp. y = 0; dialogWindow. setAttributes (lp);}/***** @ param can cancel * @ return */public TestDialog setCancelable (boolean B) {if (mDialog! = Null) {mDialog. setCancelable (B);} return this;}/*** set the dialog box title ** @ param title * @ return */public TestDialog setTitle (String title) {if (mDialog! = Null) {mTitle. setText (title); mTitle. setVisibility (View. VISIBLE);} return this;}/***** @ param: Can I cancel * @ return */public TestDialog set (boolean B) {if (mDialog! = Null) {mDialog. setCanceledOnTouchOutside (B);} return this;}/*** cancel */public void dismiss () {mDialog. dismiss ();}/*** set for loading View data ** @ param title * @ return */public TestDialog addItem (String title, Drawable drawable, OnCustomItemClickListener clickListener) {if (mItems = null) {mItems = new ArrayList
    
     
();} If (mListeners = null) {mListeners = new ArrayList
     
      
();} If (mDrawables = null) {mDrawables = new ArrayList
      
        ();} MItems. add (title); mListeners. add (clickListener); mDrawables. add (drawable); return this;} private void setItem () {if (mItems = null | mItems. size () <= 0) {return;} int count = mItems. size (); for (int I = 0; I <count; I ++) {final int index = I; TextView view = new TextView (mContext); view. setText (object + 1); view. setCompoundDrawablesRelativeWithIntrinsicBounds (null, mDrawables. get (I), null, null); view. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubmListeners. get (index ). onClick (index) ;}}); mContent. addView (view) ;}} public void show () {setItem (); mDialog. show ();} public interface OnCustomItemClickListener {public abstract void onClick (int v);} @ Overridepublic void onClick (View v) {if (v = mOk) {} else if (v = mCancel) {} mDialog. dismiss ();}}
      
     
    
   
  
 

 

Teme used:

 

Animation file: 

 

Dialog_in.xml

 

 
 
Dialog_out.xml

 

 

 
 


 

Layout file used:

 

 
     
      
       
       
       
            
             
       
        
       
      
     
    
   
  
 

 

 

Background

 

     
      
      
  
 


Use in Activity:

 

 

FindViewById (R. id. click ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubTestDialog dialog = new TestDialog (MainActivity. this); dialog. builder (R. layout. menu); dialog. setCancelable (false); dialog. addItem (Weibo 1, getResources (). getDrawable (R. drawable. ic_weibo), new OnCustomItemClickListener () {@ Overridepublic void onClick (int v) {Toast. makeText (getBaseContext (), 1, 1 ). show () ;}}); dialog. addItem (Weibo 2, getResources (). getDrawable (R. drawable. ic_weibo), new OnCustomItemClickListener () {@ Overridepublic void onClick (int v) {Toast. makeText (getBaseContext (), 3, 1 ). show () ;}}); dialog. show ();}});



 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.