public ShelfSwitchDialog(Context context) {super(context,android.R.style.Theme_NoTitleBar);setContentView(R.layout.newspaper_switch_dialog);this.context = context;Window win = getWindow();win.getAttributes().gravity = Gravity.TOP;win.setBackgroundDrawableResource(R.drawable.newspaper_blank);//大小win.setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);}

dialog的定制 :

public class ShelfSwitchDialog extends Dialog implements OnClickListener{

Context context;ImageView yingyong,shudian,baojia;int selectIndex;String[] choiceItems = new String[]{"图书","报架"};public ShelfSwitchDialog(Context context,int paddingLeft){this(context);View v = findViewById(R.id.dialog_root_layout);v.setPadding(paddingLeft-50,50,v.getPaddingRight(),v.getPaddingBottom());yingyong = (ImageView)findViewById(R.id.yingyong);yingyong.setOnClickListener(this);shudian = (ImageView)findViewById(R.id.shudian);shudian.setOnClickListener(this);baojia = (ImageView)findViewById(R.id.baojia);baojia.setOnClickListener(this);}public ShelfSwitchDialog(Context context) {super(context,android.R.style.Theme_NoTitleBar);setContentView(R.layout.newspaper_switch_dialog);this.context = context;Window win = getWindow();win.getAttributes().gravity = Gravity.TOP;win.setBackgroundDrawableResource(R.drawable.newspaper_blank);//大小win.setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);}public AlertDialog create(){if(context==null){return null;}final boolean instOfStore = context instanceof ShelfActivity||context instanceof BooksListActivity;final boolean instOfShelf = context instanceof NewspaperShelf;final Intent shelfIntent = new Intent(context,NewspaperShelf.class);final Intent storeIntent = new Intent(context,ShelfActivity.class);AlertDialog.Builder builder = new AlertDialog.Builder(context);AlertDialog dialog =  builder        .setIcon(R.drawable.newspaper_ic_switch)        .setTitle("切换到:")        .setSingleChoiceItems(choiceItems, 0, new DialogInterface.OnClickListener() {            public void onClick(DialogInterface dialog, int whichButton) {            selectIndex =  whichButton;            }        })        .setPositiveButton("确定", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int whichButton) {//            Log.v("","whichButton:"+selectIndex);            if(instOfStore){            switch(selectIndex){            case 0:            Toast.makeText(context,"当前已是书店",Toast.LENGTH_SHORT).show();            break;            case 1:            context.startActivity(shelfIntent);            break;            }            }            else if(instOfShelf){            switch(selectIndex){            case 0:            ((ShelfActivity)context).finish();            break;            case 1:            Toast.makeText(context,"当前已是报架",Toast.LENGTH_SHORT).show();            break;            }            }            }        })        .setNegativeButton("取消", new DialogInterface.OnClickListener() {            public void onClick(DialogInterface dialog, int whichButton) {                        }        })       .create();dialog.getWindow().getAttributes().gravity = Gravity.TOP;dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);return dialog;}@Overridepublic void onClick(View v) {final boolean instOfShelf = context instanceof ShelfActivity;final boolean instOfNewspaperShelf = context instanceof NewspaperShelf;final boolean instOfStoreActivity = context instanceof StoreActivity;final boolean instOfBooksListActivity = context instanceof BooksListActivity;final Intent newspaperShelfIntent = new Intent(context,NewspaperShelf.class);final Intent shelfIntent = new Intent(context,ShelfActivity.class);switch(v.getId()){case R.id.yingyong:Intent intent = new Intent("cn.chutong.ereader.finishactivity");dismiss();context.sendBroadcast(intent);break;case R.id.shudian:if(instOfNewspaperShelf){((NewspaperShelf)context).finish();dismiss();}else if(instOfShelf||instOfBooksListActivity){Toast.makeText(context,"当前已是书店",Toast.LENGTH_SHORT).show();dismiss();}else if(instOfStoreActivity){dismiss();((StoreActivity)context).finish();context.startActivity(shelfIntent);}break;case R.id.baojia:if(instOfNewspaperShelf){Toast.makeText(context,"当前已是报架",Toast.LENGTH_SHORT).show();dismiss();}else if(instOfShelf||instOfBooksListActivity){context.startActivity(newspaperShelfIntent);dismiss();}else if(instOfStoreActivity){context.startActivity(newspaperShelfIntent);dismiss();((StoreActivity)context).finish();}break;}}}

效果图:


android中定制的dialog

第二种情况:

addCustomDlg = new Dialog(this, R.style.dialog);addCustomDlg.setContentView(R.layout.add);addCustomDlg.show();confirmBtn = (Button) addCustomDlg.findViewById(R.id.confirmBtn);edit = (EditText) addCustomDlg.findViewById(R.id.txt);edit.setText("");btn_back = (ImageButton) addCustomDlg.findViewById(R.id.btn_back);btn_back.setOnClickListener(this);confirmBtn.setOnClickListener(this);

直接从dialog中获取控件,并绑定事件

效果:


android中定制的dialog

更多相关文章

  1. 获取android屏幕大小
  2. android 根据TextView宽度 自动缩小字体大小 以适应其宽度
  3. android 设置linelayout让按钮自动适应屏大小
  4. android studio 更改背景和设置字体大小
  5. android中设置AlertDialog的大小 .
  6. Android 自定义camera压缩图片到指定大小
  7. 教你如何修改Android默认字体大小和设置字体大小比例
  8. android icon 大小
  9. Android 模拟器窗口大小的修改

随机推荐

  1. (转)ANDROID强制锁定竖屏_APP固定设置竖屏
  2. Android(安卓)之 sms 短信
  3. 关于百度地图只显示中间部分,显示不全的原
  4. (一)搭建android开发环境
  5. wzplayer for android V1.5 整合硬解码(
  6. Qt for Android 编译纯C工程
  7. Android多媒体编程从初学到精通
  8. 编程回忆之Android回忆(Android Socket编
  9. Android面试题(五)—— Android的消息机制
  10. Android学习经验B