Dialog在android中的應用(一)

來源:互聯網
上載者:User

Dialog在android中的應用(一)

如果做一個如的Dialog,首先要定義樣式:

 



定義一個基類,基類裡面有一些基本方法:

 

 

public abstract class DropDownFilterDialog extends Dialog {    protected ArrayList mTopMenuList;    OnDropdownClickListener mDropdownClickListener;    protected LinearLayout mDropDownMenuLayout;    public Context mContext;    private int mCurrentIndex = 0;    public void setCurrentIndex(int mCurrentIndex) {        this.mCurrentIndex = mCurrentIndex;    }    public void setTopMenuItemList(ArrayList mTopMenuList) {        this.mTopMenuList = mTopMenuList;    }    public DropDownFilterDialog(Context context) {        super(context, R.style.DropdownFilterDialogStyle);        mContext = context;    }    public interface OnDropdownClickListener {        void onDropdownHide();        void onClickItem(int index);    }    public void setDropDownClickListener(OnDropdownClickListener dropdownClickListener) {        mDropdownClickListener = dropdownClickListener;    }    public abstract void hideDropDownFilter(boolean showAnimation);    public abstract void showDialog(boolean showAnimation, View banner);    public void init() {        mDropDownMenuLayout = (LinearLayout) findViewById(R.id.text_chat_menu_linear);        constructButton(mTopMenuList);    }    @Override    protected void onStart() {        super.onStart();        init();    }    public void constructButton(ArrayList list) {        mDropDownMenuLayout.removeAllViews();        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//        mDropDownMenuLayout.addView(View.inflate(mContext,//                R.layout.view_top_menu_separator, null), params);        final int size = list.size();        final int lastPosition = size - 1;        for (int i = 0; i < size; i++) {            DropDownItem item = list.get(i);            View view = View.inflate(mContext, R.layout.dropdown_menu_item_layout,                    null);            view.setTag(i);            view.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                    hideDropDownFilter(true);                    mDropdownClickListener.onClickItem((Integer) view.getTag());                }            });            TextView btn = (TextView) view                    .findViewById(R.id.top_menu_button);            btn.setText(item.getName());            btn.setTag(item.getIndex());            if (mCurrentIndex == i) {                btn.setTextColor(mContext.getResources().getColor(R.color.contact_detail_phone_related_color_pressed));            }            TextView tab_indicator_counter = (TextView) view.findViewById(R.id.indicator_counter);            if (item.getCouter() == 0) {                tab_indicator_counter.setVisibility(View.GONE);            } else {                tab_indicator_counter.setText(String.valueOf(item.getCouter()));            }            boolean showSeparator = true;            if (size == 1) {                showSeparator = false;            } else if (i == lastPosition) {                showSeparator = false;            }            view.setBackgroundResource(R.drawable.drop_down_filter_item_background_selector);            mDropDownMenuLayout.addView(view, params);            if (showSeparator) {                LinearLayout.LayoutParams paramsForLine = new LinearLayout.LayoutParams(                        WindowManager.LayoutParams.MATCH_PARENT, mContext.getResources().getDimensionPixelSize(R.dimen.top_menu_vertical_separator));                paramsForLine.leftMargin = mContext.getResources().getDimensionPixelSize(R.dimen.drop_down_menu_item_margin_left_right);                paramsForLine.rightMargin = paramsForLine.leftMargin;                mDropDownMenuLayout.addView(View.inflate(mContext,                        R.layout.drop_down_menu_divider_layout, null), paramsForLine);            }        }        mDropDownMenuLayout.invalidate();    }}


 

然後是具體實作類別:

 

public class DropDownFilterDialogForTablet extends DropDownFilterDialog {    public DropDownFilterDialogForTablet(Context context) {        super(context);        mContext = context;        Window window = getWindow();        WindowManager.LayoutParams lp = window.getAttributes();        lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.drop_down_filter_dialog);    }    @Override    protected void onStart() {        super.onStart();        init();    }    public void hideDropDownFilter(boolean showAnimation) {        dismiss();        mDropdownClickListener.onDropdownHide();    }    public void init() {        super.init();        mDropDownMenuLayout.setBackgroundResource(R.drawable.dropdown_filters_background);    }    public void showDialog(boolean showAnimation, View banner) {        Window window = getWindow();        WindowManager.LayoutParams lp = window.getAttributes();        int height = mContext.getResources().getDimensionPixelSize(R.dimen.drop_down_menu_item_margin_top_for_tablet);        if (banner == null) {            lp.y = height;        } else {            lp.y = height + banner.getMeasuredHeight();        }        show();    }    @Override    public boolean onTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            hideDropDownFilter(true);            return true;        }        return false;    }    @Override    public boolean dispatchKeyEvent(KeyEvent event) {        if (event.getAction() == KeyEvent.ACTION_DOWN                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {            hideDropDownFilter(true);            return true;        }        return super.dispatchKeyEvent(event);    }}


 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.