zoukankan      html  css  js  c++  java
  • 继承LinearLayout自定义左侧菜单

    public class LeftMenuView extends LinearLayout {
        LinkedHashMap<Integer,String> map=new LinkedHashMap<>();
        public LeftMenuView(Context context) {
            super(context);
            addView(context);
        }
        public LeftMenuView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            addView(context);
        }
        public LeftMenuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            addView(context);
        }
    
        private void addView(final Context mContext) {
            map.put(R.drawable.ico_left_menu_cashier,"收银");
            map.put(R.drawable.ico_left_menu_order,"订单");
            map.put(R.drawable.ico_left_menu_after_sales,"售后");
            map.put(R.drawable.ico_left_menu_goods,"商品");
            map.put(R.drawable.ico_left_menu_setting,"设置");
            for (final Map.Entry<Integer, String> entry : map.entrySet()) {
                TextView tvName=new TextView(mContext);
                View viewLine=new View(mContext);
                addView(tvName);
                addView(viewLine);
    
                LinearLayout.LayoutParams tvLayoutParams = (LayoutParams) tvName.getLayoutParams();
                LinearLayout.LayoutParams viewLayoutParams = (LayoutParams) viewLine.getLayoutParams();
    
                tvLayoutParams.width=LayoutParams.WRAP_CONTENT;
                tvLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.left_menu_tv_heigth);
                tvLayoutParams.gravity=Gravity.CENTER;
                tvName.setText(entry.getValue());
                tvName.setTag(entry.getKey());
                tvName.setTextSize(getResources().getDimension(R.dimen.textsize_10));
                tvName.setTextColor(ContextCompat.getColor(mContext,R.color.common_blue));
                tvName.setGravity(Gravity.CENTER);
                Drawable drawableLeft=ContextCompat.getDrawable(mContext,entry.getKey());
                drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
                tvName.setCompoundDrawables(drawableLeft,null,null,null);
                tvName.setCompoundDrawablePadding(getResources().getDimensionPixelSize(R.dimen._10dp));
    
                viewLayoutParams.width= LayoutParams.MATCH_PARENT;
                viewLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.common_margin_super_small);
                viewLine.setBackgroundColor(ContextCompat.getColor(mContext,R.color.divider_line_grey_color));
                tvName.setOnClickListener(new View.OnClickListener(){
    
                    @Override
                    public void onClick(View v) {
                        //listener.leftClick();
                        int drawableId=Integer.valueOf(v.getTag().toString());
                        if(drawableId==R.drawable.ico_left_menu_cashier){
                            mContext.startActivity(new Intent(mContext, MainActivity.class));
                        }else if(drawableId==R.drawable.ico_left_menu_order){
    
                        }else if(drawableId==R.drawable.ico_left_menu_after_sales){
    
                        }else if(drawableId==R.drawable.ico_left_menu_goods){
                            mContext.startActivity(new Intent(mContext, GoodsMessageActivity.class));
                        }else if(drawableId==R.drawable.ico_left_menu_setting){
    
                        }
                    }
                });
                tvName.setLayoutParams(tvLayoutParams);
                viewLine.setLayoutParams(viewLayoutParams);
            }
        }
    
    
        //自定义的顶部ActionBar的点击监听;
        private LeftMenuClickListener listener;
        //点击事件的监听接口
        public interface LeftMenuClickListener {
            void leftClick();
            void rightClick();
        }
        //提供activity调用的方法,类似于Button类的setOnClickListener(OnClickListener listener)
        //传入具体实现方法
        public void setOnTopbarClickListener(LeftMenuClickListener listener){
            this.listener=listener;
        }
    
    }
    View Code

    使用方法

        <com.freexiaoyu.app.widget.LeftMenuView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@id/linear_leftmenu"
            android:layout_width="220.0dip"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="@color/white"
            android:orientation="vertical" />
    

      

  • 相关阅读:
    TCP 的那些事儿(转载)
    3. 对象在内存中的布局
    GO语言学习之数据类型-->基本类型(字符串)
    GO语言学习之变量and常量
    wrk
    为什么显示消息“错误:您所在国家/地区是禁运国,无法下载 Java”?
    raw.githubusercontent.com 访问不了
    Windows Terminal
    vue:无法加载文件C:UsersAppDataRoaming pmvue.ps1, 在此系统上无法加载脚本
    vue使用过滤改变el-switch开关的状态
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/9778561.html
Copyright © 2011-2022 走看看