首页 > 学院 > 开发设计 > 正文

自定义Dialog之底部弹出Dialog

2019-11-09 18:27:20
字体:
来源:转载
供稿:网友

先预览下效果:

自定义Dialog,设置Dialog居于底部并且和屏幕一样宽:

public class BottomStyleDialog extends Dialog implements OnItemClickListener {    PRivate String[] mNames = new String[] {            "我是条目1", "我是条目2", "我是条目3", "我是条目4", "我是条目5","我是条目6","我是条目7"    };    private ListView mLv;    private ArrayList<StyleItem> mList = new ArrayList<StyleItem>();    private StyleAdapter mAdapter;    public BottomStyleDialog(Context context) {        // 在构造方法里, 传入主题        super(context, R.style.BottomDialogStyle);        // 拿到Dialog的Window, 修改Window的属性        Window window = getWindow();        window.getDecorView().setPadding(0, 0, 0, 0);        // 获取Window的LayoutParams        LayoutParams attributes = window.getAttributes();        attributes.width = LayoutParams.MATCH_PARENT;        attributes.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;        // 一定要重新设置, 才能生效        window.setAttributes(attributes);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.view_dialog);        initView();        initData();    }    private void initView() {        mLv = (ListView) findViewById(R.id.lv_view_dialog);        mLv.setOnItemClickListener(this);    }    private void initData() {        // 填充数据集合        for (int i = 0; i < mNames.length; i++) {            StyleItem styleItem = new StyleItem();            styleItem.mName = mNames[i];            mList.add(styleItem);        }        mAdapter = new StyleAdapter(getContext(), mList);        mLv.setAdapter(mAdapter);    }    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        mAdapter.notifyDataSetChanged();    }}styles中定义Dialog的样式及其进入和退出动画

<!-- 自定义Dialog主题 --><style name="BottomDialogStyle" parent="@android:style/Theme.Dialog">	<item name="android:windowNoTitle">true</item>	<item name="android:windowBackground">@android:color/white</item>	<item name="android:windowAnimationStyle">@style/BottomDialogAnimation</item></style><!-- 自定义Dialog动画 --><style name="BottomDialogAnimation">	<item name="android:windowEnterAnimation">@anim/input_method_enter</item>	<item name="android:windowExitAnimation">@anim/input_method_exit</item></style>Dialog中使用的Adapter:

public class StyleAdapter extends BaseAdapter {    private Context mContext;    private ArrayList<StyleItem> mList;    public StyleAdapter(Context context, ArrayList<StyleItem> list) {        mContext = context;        mList = list;    }    @Override    public int getCount() {        return mList.size();    }    @Override    public StyleItem getItem(int position) {        return mList.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder;        if (convertView == null) {            convertView = View.inflate(mContext, R.layout.item_dialog_style, null);            holder = new ViewHolder();            holder.tvName = (TextView) convertView.findViewById(R.id.tv_item_dialog_name);            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        StyleItem item = getItem(position);        holder.tvName.setText(item.mName);        return convertView;    }    static class ViewHolder {        TextView tvName;    }}点击展示Dialog

BottomStyleDialog bottomStyleDialog = new BottomStyleDialog(MainActivity.this);bottomStyleDialog.show();代码GitHub地址


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表