Android 自定义底部公用菜单
2013-07-04 15:37
162 查看
注释:此案例主要展示自定义底部菜单,一处封装处处调用。使用起来相当方便
一、初始的Activity
第二个Activity
自定义Menu
一下是布局文件
完毕
一、初始的Activity
package com.example.myapi.buttommenu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.example.myapi.R; import com.example.myapi.buttommenu.MenuUtils.MeunOnClickListener; public class Menu1Activity extends Activity implements MeunOnClickListener,OnClickListener{ private MenuUtils menuUtils; private Button btn_two; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu1); menuUtils = (MenuUtils)findViewById(R.id.menu_utils); menuUtils.setMenuListener(this); btn_two = (Button)findViewById(R.id.btn_two); btn_two.setOnClickListener(this); } @Override public void menuOne() { Toast.makeText(this, "您点击了菜单一", Toast.LENGTH_LONG).show(); } @Override public void menuTwo() { Toast.makeText(this, "您点击了菜单二", Toast.LENGTH_LONG).show(); } @Override public void menuThree() { Toast.makeText(this, "您点击了菜单三", Toast.LENGTH_LONG).show(); } @Override public void menuFour() { Toast.makeText(this, "您点击了菜单四", Toast.LENGTH_LONG).show(); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_two: Intent intent = new Intent(); intent.setClass(this, Menu2Activity.class); startActivity(intent); break; } } }
第二个Activity
package com.example.myapi.buttommenu; import com.example.myapi.R; import com.example.myapi.buttommenu.MenuUtils.MeunOnClickListener; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; public class Menu2Activity extends Activity implements MeunOnClickListener{ private MenuUtils menuUtils; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu2); menuUtils = (MenuUtils)findViewById(R.id.menu_utils); menuUtils.setMenuListener(this); } @Override public void menuOne() { Toast.makeText(this, "您点击了菜单一", Toast.LENGTH_LONG).show(); } @Override public void menuTwo() { Toast.makeText(this, "您点击了菜单二", Toast.LENGTH_LONG).show(); } @Override public void menuThree() { Toast.makeText(this, "您点击了菜单三", Toast.LENGTH_LONG).show(); } @Override public void menuFour() { Toast.makeText(this, "您点击了菜单四", Toast.LENGTH_LONG).show(); } }
自定义Menu
package com.example.myapi.buttommenu; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import com.example.myapi.R; /** * 菜单工具类 * @author * * */ public class MenuUtils extends LinearLayout implements OnClickListener{ private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Context context; private MeunOnClickListener listener; public MenuUtils(Context context) { super(context); this.context = context; initView(context); } public MenuUtils(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } private void initView(Context context){ LinearLayout menuView = (LinearLayout)LayoutInflater.from(context).inflate(com.example.myapi.R.layout.menutuils, null); addView(menuView); btn_1 = (Button)menuView.findViewById(R.id.btn_1); btn_2 = (Button)menuView.findViewById(R.id.btn_2); btn_3 = (Button)menuView.findViewById(R.id.btn_3); btn_4 = (Button)menuView.findViewById(R.id.btn_4); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); } public void setMenuListener(MeunOnClickListener listener){ this.listener = listener; } public interface MeunOnClickListener{ public void menuOne(); public void menuTwo(); public void menuThree(); public void menuFour(); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_1: listener.menuOne(); break; case R.id.btn_2: listener.menuTwo(); break; case R.id.btn_3: listener.menuThree(); break; case R.id.btn_4: listener.menuFour(); break; } } }
一下是布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.myapi.buttommenu.MenuUtils android:id="@+id/menu_utils" android:layout_width="fill_parent" android:layout_height="60dp" /> <Button android:id="@+id/btn_two" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="跳转到第二个界面"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.myapi.buttommenu.MenuUtils android:id="@+id/menu_utils" android:layout_width="fill_parent" android:layout_height="60dp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="菜单一"/> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:text="菜单二"/> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:text="菜单三"/> <Button android:id="@+id/btn_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:text="菜单四"/> </LinearLayout> </LinearLayout>
完毕
相关文章推荐
- 【iPhone Demo】地图自定义大头针如何利用Android TabHost+ActivityGroup+Broadcast实现类似于iPhone的底部菜单
- Android笔记__PopupWindow底部弹出自定义菜单
- Android自定义View之实现流行的新浪微博底部菜单:高仿“咸鱼APP”的底部菜单动画效果。
- android开发 - 自定义 弹出 底部菜单
- Android实现自定义菜单、底部导航条(一)
- Android自定义View之实现流行的底部菜单栏中间突起:高仿“咸鱼APP”的底部菜单。
- Android自定义Dialogの仿ios系统底部菜单(自下而上弹出)
- Android笔记__PopupWindow底部弹出自定义菜单
- [置顶] Android:PopWindow — 对Android的底部弹窗、顶部弹窗菜单及自定义界面的使用封装
- Android 分页控件制成底部菜单.
- android 自定义菜单 使用PopupWindow实现菜单的各种效果
- android开发,自定义左侧滑出菜单
- 一起学android之底部菜单TabHost的实现(9)
- android 底部菜单
- android自定义PopWindow底部显示
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android 自定义菜单
- android自定义底部弹出对话框
- android acitonbar(toolbar)自定义溢出菜单样式
- android自定义view侧滑菜单