魔乐科技安卓开发教程----李兴华----03菜单

1.ActivityGroup+GridView方式的基本菜单

1、创建用于显示icon的activity:MyActivity及其布局文件mylayout.xml
mylayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/dress01"/>
</LinearLayout>

*MyActivity//只是一个壳子

public class MyActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.mylayout);
    }
}

2、定义整体程序的主布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/content"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </LinearLayout>
        <GridView
            android:id="@+id/gridviewbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:fadingEdgeLength="5dp"
            android:fadingEdge="vertical"/>


    </RelativeLayout>

</LinearLayout>

3、定义baseadpter类的子类MenuImageAdapter

public class MenuImageAdapter extends BaseAdapter {
    private ImageView[] menuImg=null;
    private Context context=null;
    private  int selectedMenuImage=R.drawable.selectimage;
    public MenuImageAdapter (Context context,int imageIds[], int wdith,int height,int selectedMenuImage){
        this.context=context;
        this.selectedMenuImage=selectedMenuImage;
        this.menuImg=new ImageView[imageIds.length];//开辟新的数组
        for (int i=0; i<imageIds.length;i++){
            this.menuImg[i]=new ImageView(this.context);//实例化imageView数组
            this.menuImg[i].setLayoutParams(new GridView.LayoutParams(wdith,height));//设置图片的大小
            this.menuImg[i].setAdjustViewBounds(false);//不调整边界显示
            this.menuImg[i].setPadding(3,3,3,3);//设置边距
            this.menuImg[i].setImageResource(imageIds[i]);

        }


    }
    @Override
    public int getCount() {
        return this.menuImg.length;
    }

    @Override
    public Object getItem(int position) {
        return this.menuImg[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView=null;
        if (convertView==null){
            imageView=this.menuImg[position];

        }else{
            imageView= (ImageView) convertView;
        }

        return imageView;
    }

    public void setFocus(int selectId){//设置选中的显示
        for (int i=0;i<this.menuImg.length;i++){
            if (i!=selectId){
                this.menuImg[i].setBackgroundResource(0);//不设置背景图
            }
        }
        this.menuImg[selectId].setBackgroundResource(this.selectedMenuImage);//设置选中状态下的背景图
    }
}

4、主activity文件

public class MainActivity extends ActivityGroup {
    private GridView gridviewbar=null;
    private MenuImageAdapter menu=null;
    private LinearLayout content=null;
    //填充图片的资源
    private int menu_image[]=new int[]{R.drawable.dress01,R.drawable.dress02,R.drawable.dress03,R.drawable.dress04,R.drawable.dress05};
    private  int width=0,height=0;//求出平均的宽高,用于定位
    private Intent intent=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题
        setContentView(R.layout.activity_main);

        gridviewbar=findViewById(R.id.gridviewbar);
        content=findViewById(R.id.content);

        //定义工具栏的信息显示
        gridviewbar.setNumColumns(menu_image.length);//设置工具栏(下排导航条)的栏数
        gridviewbar.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的设置背景透明
        gridviewbar.setGravity(Gravity.CENTER);
        gridviewbar.setVerticalSpacing(0);
        width=getWindowManager().getDefaultDisplay().getWidth()/menu_image.length;
        height=getWindowManager().getDefaultDisplay().getHeight()/10;

        this.menu=new MenuImageAdapter(this,menu_image,width, height,R.drawable.dress01);
        gridviewbar.setAdapter(this.menu);//设置activity的adapter
        switchActivity(0);//设置第一个被选中
        gridviewbar.setOnItemClickListener(new OnItemClickListenerImp());

    }

    private void switchActivity(int id){//切换选中的操作
        menu.setFocus(id);//设置选中图片的背景(自定义的方法,来自MenuImageAdapter)
        content.removeAllViews();//content的LinearLayout布局上删除所有的内容
        switch (id){//根据选中的id跳转到对应的activity
            case 0:
                intent =new Intent(this,MyActivity.class);
                break;
            case 1:
                intent =new Intent(this,MyActivity.class);
                break;
            case 2:
                intent =new Intent(this,MyActivity.class);
                break;
            case 3:
                intent =new Intent(this,MyActivity.class);
                break;
            case 4:
                exitDailog();
               return;
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Window subActivity=getLocalActivityManager().startActivity("subactivity",intent);
        //设置图片在主布局中的显示
        content.addView(subActivity.getDecorView(), LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        //注册时间
        gridviewbar.setOnItemClickListener(new OnItemClickListenerImp());
    }

    //事件处理
    private class OnItemClickListenerImp implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MainActivity.this.switchActivity(position);
        }
    }
    private void exitDailog(){
        Dialog dialog=new AlertDialog.Builder(this).setIcon(R.drawable.dress05).setTitle("程序退出")
                .setMessage("请确实退出本程序吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.switchActivity(0);
                    }
                }).create();
        dialog.show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            exitDailog();
        }
        return false;
    }
}

结果:
在这里插入图片描述

2.POPUpWindow弹出菜单

5、弹出菜单的组成内容:
在这里插入图片描述
6、添加string到资源文件
String.xml内容

Hello world! My popMenu Demo
MyEX007PopMenu
常用
设置
工具

7、创建窗口标题PopMenuTitleAdapter继承BaseAdapter

public class PopMenuTitleAdapter extends BaseAdapter {
    private TextView menuTitle[]=null;
    private int fontColor,selectedColor,unSelectedColor;
    public PopMenuTitleAdapter(Context context,int [] titleIds,int fontSize,int fontColor,int selectedColor,int unSelectedColor){
        this.fontColor=fontColor;
        this.selectedColor=selectedColor;
        this.unSelectedColor=unSelectedColor;
        this.menuTitle=new TextView[titleIds.length];
        for (int i = 0; i <titleIds.length  ; i++) {
            this.menuTitle[i]=new TextView(context);
            this.menuTitle[i].setText(titleIds[i]);
            this.menuTitle[i].setTextSize(fontSize);
            this.menuTitle[i].setGravity(Gravity.CENTER);
            this.menuTitle[i].setPadding(10,10,10,10);
        }
    }

    @Override
    public int getCount() {
        return menuTitle.length;
    }

    @Override
    public Object getItem(int position) {
        return menuTitle[position];
    }

    @Override
    public long getItemId(int position) {
        return menuTitle[position].getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        if (convertView==null){
            view=menuTitle[position];
        }else{
            view=convertView;
        }
        return view;
    }
    //设置焦点菜单(标题)的背景色
    public void setFocus(int index){
        for (int i = 0; i <menuTitle.length ; i++) {
            if (i!=index){
                menuTitle[i].setBackgroundDrawable(new ColorDrawable(unSelectedColor));
                menuTitle[i].setTextColor(fontColor);
            }
        }
        menuTitle[index].setBackgroundColor(0x00);
        menuTitle[index].setTextColor(selectedColor);
    }
}

8、创建窗口主体PopMenuBodyAdapter继承BaseAdapter

public class PopMenuBodyAdapter extends BaseAdapter {
    private ImageView[] menuImg=null;
    public PopMenuBodyAdapter(Context context,int[] picIds){
        this.menuImg=new ImageView[picIds.length];
        for (int i = 0; i <menuImg.length ; i++) {
            this.menuImg[i]=new ImageView(context);
            this.menuImg[i].setImageResource(picIds[i]);
        }

    }

    @Override
    public int getCount() {
        return menuImg.length;
    }

    @Override
    public Object getItem(int position) {
        return menuImg[position];
    }

    @Override
    public long getItemId(int position) {
        return menuImg[position].getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        if (convertView==null){
            view=menuImg[position];
        }else{
            view=convertView;
        }
        return view;
    }
}

9、扩充PopMenu 组件继承PopWindow

public class PopMenu extends PopupWindow {
    private GridView popTitle,popBody;//标题集主体
    private PopMenuTitleAdapter titleAdapter=null;
    private LinearLayout layout=null;

    public PopMenu(Context context, int titleIds[],int backgroundColor, AdapterView.OnItemClickListener titleCallBack, AdapterView.OnItemClickListener bodyCallBack){
        super(context);//创建弹出的组件
        this.titleAdapter=new PopMenuTitleAdapter(context,titleIds,16,Color.BLACK, Color.LTGRAY,Color.WHITE);
        this.layout=new LinearLayout(context);
        this.layout.setOrientation(LinearLayout.VERTICAL);
        popTitle=new GridView(context);
        popTitle.setLayoutParams(new Toolbar.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        this.popTitle.setNumColumns(titleIds.length);
        this.popTitle.setHorizontalSpacing(1);
        this.popTitle.setVerticalSpacing(1);
        this.popTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);//拉伸到列宽
        popTitle.setAdapter(titleAdapter);
        //标题的回调,bodyCallback是参数里的
        popTitle.setOnItemClickListener(titleCallBack );

        this.popBody=new GridView(context);
        this.popBody.setLayoutParams(new Toolbar.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        this.popBody.setNumColumns(5);
        this.popBody.setHorizontalSpacing(1);
        this.popBody.setVerticalSpacing(1);
        this.popBody.setPadding(10,10,10,10);
        this.popBody.setGravity(Gravity.CENTER);
        this.popBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);//拉伸到列宽
        popBody.setOnItemClickListener(bodyCallBack );


        this.layout.addView(this.popTitle);//将标题和窗体添加到线性布局上layout
        this.layout.addView(this.popBody);
        super.setContentView(this.layout);//设置activity的布局
        super.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        super.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        super.setBackgroundDrawable(new ColorDrawable(backgroundColor));
        super.setFocusable(true);
    }

    public void setPopMenuBodyAdapter(PopMenuBodyAdapter adapter){

        this.popBody.setAdapter(adapter);

    }
    //设置标题选择
    public void setPopTitleSelected(int position){
      popTitle.setSelection(position);
      titleAdapter.setFocus(position);
    }
    //设置选中大项的背景色
    public void setPopBodySelected(int position,int seclectColor){
        int count=popBody.getChildCount();
        for (int i = 0; i < count; i++) {
            if (i!=position){
                ImageView img= (ImageView) popBody.getChildAt(position);
                img.setBackgroundColor(Color.TRANSPARENT);
            }
        }
        ImageView img= (ImageView) popBody.getChildAt(position);
        img.setBackgroundColor(seclectColor);
    }
}

10、修改主activity文件

public class MainActivity extends ActivityGroup {
    private GridView gridviewbar=null;
    private MenuImageAdapter menu=null;
    private LinearLayout content=null;
    //填充图片的资源
    private int menu_image[]=new int[]{R.drawable.dress01,R.drawable.dress02,R.drawable.dress03,R.drawable.dress04,R.drawable.dress05};
    private  int width=0,height=0;//求出平均的宽高,用于定位
    private Intent intent=null;
    private boolean isShow=false;

    private  int CommonItemids[]=new  int[] {R.drawable.dress11,R.drawable.dress12,R.drawable.dress13,R.drawable.dress14,R.drawable.dress15,
            R.drawable.dress21,R.drawable.dress21,R.drawable.dress23};
    private  int setItemids[]=new  int[] {R.drawable.dress24,R.drawable.dress25,R.drawable.dress31,
            R.drawable.dress01,R.drawable.dress02,R.drawable.dress03,R.drawable.dress04,R.drawable.dress05};
    private  int toolItemids[]=new  int[] {R.drawable.dress01,R.drawable.dress11,R.drawable.dress21,
            R.drawable.dress31,R.drawable.dress02,R.drawable.dress22,R.drawable.dress13,R.drawable.dress23};
    private PopMenu popMenu=null;
    private int titleIds[]=new int[]{R.string.popmenu_common,R.string.popmenu_set,R.string.popmenu_tool};
    public PopMenuBodyAdapter commonAdapter=null;
    private PopMenuBodyAdapter settAdapter=null;
    private PopMenuBodyAdapter toolAdapter=null;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题
        setContentView(R.layout.activity_main);

        gridviewbar=findViewById(R.id.gridviewbar);
        content=findViewById(R.id.content);

        //定义工具栏的信息显示
        gridviewbar.setNumColumns(menu_image.length);//设置工具栏(下排导航条)的栏数
        gridviewbar.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的设置背景透明
        gridviewbar.setGravity(Gravity.CENTER);
        gridviewbar.setVerticalSpacing(0);
        width=getWindowManager().getDefaultDisplay().getWidth()/menu_image.length;
        height=getWindowManager().getDefaultDisplay().getHeight()/8;

        this.menu=new MenuImageAdapter(this,menu_image,width, height,R.drawable.dress01);
        gridviewbar.setAdapter(this.menu);//设置activity的adapter
        switchActivity(0);//设置第一个被选中

        this.popMenu=new PopMenu(this,this.titleIds,0x55123456,new PopTitleOnItemClickListenerCallback(),new PopBodyOnItemClickListenerCallback());

        this.commonAdapter=new PopMenuBodyAdapter(this,CommonItemids);
        this.settAdapter=new PopMenuBodyAdapter(this,setItemids);
        this.toolAdapter=new PopMenuBodyAdapter(this,toolItemids);

        this.popMenu.setPopMenuBodyAdapter(commonAdapter);
        popMenu.setPopTitleSelected(0);

        gridviewbar.setOnItemClickListener(new OnItemClickListenerImp());

    }

    private void switchActivity(int id){//切换选中的操作
        menu.setFocus(id);//设置选中图片的背景(自定义的方法,来自MenuImageAdapter)
        content.removeAllViews();//content的LinearLayout布局上删除所有的内容
        switch (id){//根据选中的id跳转到对应的activity
            case 0:
                intent =new Intent(this,MyActivity.class);
                break;
            case 1:
                intent =new Intent(this,MyActivity.class);
                break;
            case 2:
                intent =new Intent(this,MyActivity.class);
                break;
            case 3:
                showPopupMenu();
                break;
            case 4:
                exitDailog();
               return;
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Window subActivity=getLocalActivityManager().startActivity("subactivity",intent);
        //设置图片在主布局中的显示
        content.addView(subActivity.getDecorView(), LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        //注册时间
        gridviewbar.setOnItemClickListener(new OnItemClickListenerImp());
    }

    //事件处理
    private class OnItemClickListenerImp implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MainActivity.this.switchActivity(position);
        }
    }
    private void exitDailog(){
        Dialog dialog=new AlertDialog.Builder(this).setIcon(R.drawable.dress05).setTitle("程序退出")
                .setMessage("请确实退出本程序吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.switchActivity(0);
                    }
                }).create();
        dialog.show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            exitDailog();
        }
        return false;
    }

    public void showPopupMenu(){
        if (this.isShow){//已经显示了
            popMenu.dismiss();
            isShow=false;
        }else{

            this.popMenu.showAtLocation(gridviewbar,Gravity.BOTTOM ,0,height);
            isShow=true;
        }

    }

    private class PopTitleOnItemClickListenerCallback implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MainActivity.this.popMenu.setPopTitleSelected(position);
            switch (position){
                case 0:
                    MainActivity.this.popMenu.setPopMenuBodyAdapter(commonAdapter);
                    break;
                case 1:
                    MainActivity.this.popMenu.setPopMenuBodyAdapter(settAdapter);
                    break;
                case 2:
                    MainActivity.this.popMenu.setPopMenuBodyAdapter(toolAdapter);
                    break;

            }
        }
    }

    private class PopBodyOnItemClickListenerCallback implements AdapterView.OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MainActivity.this.popMenu.setPopBodySelected(position,Color.GRAY);
            Toast.makeText(MainActivity.this, "执行选项:"+position, Toast.LENGTH_SHORT).show();

        }
    }
}

结果:
在这里插入图片描述
项目文件见附件:
https://download.csdn.net/download/weixin_43745804/12250476

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值