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

使用方法html

    <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" />
相關文章
相關標籤/搜索