1. 程式人生 > >繼承LinearLayout自定義左側選單

繼承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; } }