轻松实现dialog弹窗
记录一下传统的实现弹窗方式,实现一个简单的弹窗效果;
要实现一个弹窗我们首先要创建一个类去继承dialog,主题theme需要在style文件中添加以下标签。
<style name="Dialog" parent="android:style/Theme.Dialog"> <item name="android:background">#00000000</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> </style>有了这个我们就可以进行下一步:定义一个类
/** * dialog * Created by Administrator on 2017/8/4. */ public class DialogAssure extends Dialog { private Context context; private View assureView; ProductServiceAssureAdapter assureAdapter; //构造 public DialogAssure(@NonNull Context context,List<Map<String,String>> listSafe) { super(context, R.style.Dialog); this.context=context; assureView= LayoutInflater.from(context).inflate(R.layout.dialog_assure_layout,null); setContentView(assureView); //noinspection ConstantConditions getWindow().getAttributes().gravity = Gravity.BOTTOM; RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); LinearLayoutManager layoutManager=new LinearLayoutManager(context); recyclerView.setLayoutManager(layoutManager); assureAdapter=new ProductServiceAssureAdapter(context,listSafe,2); recyclerView.setAdapter(assureAdapter); } @Override public void show() { Display display = ((Activity)context).getWindowManager().getDefaultDisplay(); @SuppressWarnings("ConstantConditions") WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.width = display.getWidth(); //设置宽度 getWindow().setAttributes(lp); super.show(); } //选择框向上弹出动画 @Override protected void onStart() { Animation anim = AnimationUtils.loadAnimation(getContext(),R.anim.push_bottom_in); assureView.startAnimation(anim); super.onStart(); } }
接下来就是在你要用的地方进行实例化
new Dialog();//这里括号里传对应于构造方法中所需要的数据
效果图如下: