Dialog在android中的应用(一)

如果做一个如下图的Dialog,首先要定义样式:

 <style name="DropdownFilterDialogStyle" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowSoftInputMode">stateUnchanged|adjustResize</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

定义一个基类,基类里面有一些基本方法:

public abstract class DropDownFilterDialog extends Dialog {

    protected ArrayList<DropDownItem> 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<DropDownItem> mTopMenuList) {
        this.mTopMenuList = mTopMenuList;
    }

    public DropDownFilterDialog(Context context) {
       <strong> super(context, R.style.DropdownFilterDialogStyle);</strong>
        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<DropDownItem> 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) {
       <strong> 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();
        }</strong>
        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);
    }

}







技术分享



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。