首页 > 代码库 > android自定义Dialog菜鸟级讲解

android自定义Dialog菜鸟级讲解

PopuWindowHealthRecord 这是dialog
PopuWindowHealthRecord.this.dismiss();
PopuWindowHealthRecord extends Dialog
dialog设置自定义风格(样式)
public PopuWindowHealthRecord(Context context) {
super(context, R.style.HealthDialog);
//设置布局(完美实现各种布局)dialog和activity就基本差不多了
setContentView(R.layout.popuwindow_health_record);
this.inflater = LayoutInflater.from(context);
dialog自定义风格values下的stytle。xml中写
<style name="HealthDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>

<item name="android:windowBackground">@android:color/background_light</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
创建dialog(实例化)
//我的健康档案
tvHealthRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(contentView.getContext(), "测试点击", Toast.LENGTH_LONG).show();
//创建POP窗口
PopuWindowHealthRecord popuWindowHealthRecord = new PopuWindowHealthRecord(context);
//设置布局属性用的parm
WindowManager.LayoutParams parm = new WindowManager.LayoutParams();
// 获取屏幕密度
DisplayMetrics dm = new DisplayMetrics();
dm = context.getResources().getDisplayMetrics();
//设置屏幕宽高
parm.width =dm.widthPixels/2;
parm.height = dm.heightPixels;
//设置dialog位置
//这个设置是紧靠左边
parm.x = -parm.width;
parm.y = 0;
popuWindowHealthRecord.getWindow().setAttributes(parm);
//设置是否点击dialog外的地方,dialog消失
popuWindowHealthRecord.setCanceledOnTouchOutside(false);
popuWindowHealthRecord.show();

 

android自定义Dialog菜鸟级讲解