- 浏览: 576039 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (669)
- oracle (36)
- java (98)
- spring (48)
- UML (2)
- hibernate (10)
- tomcat (7)
- 高性能 (11)
- mysql (25)
- sql (19)
- web (42)
- 数据库设计 (4)
- Nio (6)
- Netty (8)
- Excel (3)
- File (4)
- AOP (1)
- Jetty (1)
- Log4J (4)
- 链表 (1)
- Spring Junit4 (3)
- Autowired Resource (0)
- Jackson (1)
- Javascript (58)
- Spring Cache (2)
- Spring - CXF (2)
- Spring Inject (2)
- 汉字拼音 (3)
- 代理模式 (3)
- Spring事务 (4)
- ActiveMQ (6)
- XML (3)
- Cglib (2)
- Activiti (15)
- 附件问题 (1)
- javaMail (1)
- Thread (19)
- 算法 (6)
- 正则表达式 (3)
- 国际化 (2)
- Json (3)
- EJB (3)
- Struts2 (1)
- Maven (7)
- Mybatis (7)
- Redis (8)
- DWR (1)
- Lucene (2)
- Linux (73)
- 杂谈 (2)
- CSS (13)
- Linux服务篇 (3)
- Kettle (9)
- android (81)
- protocol (2)
- EasyUI (6)
- nginx (2)
- zookeeper (6)
- Hadoop (41)
- cache (7)
- shiro (3)
- HBase (12)
- Hive (8)
- Spark (15)
- Scala (16)
- YARN (3)
- Kafka (5)
- Sqoop (2)
- Pig (3)
- Vue (6)
- sprint boot (19)
- dubbo (2)
- mongodb (2)
最新评论
1.自定义Diolog类
2.弹出框所使用的布局文件
3.activity内容
使用替换layout布局文件的方法
页面布局
selector
调用方法
转自:http://my.oschina.net/u/1036767/blog/266318?p=1
package com.test.widget; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import com.test.R; /** * 自定义弹出框 * * @author Administrator * */ @SuppressWarnings("all") public class CustomDialog extends Dialog { public CustomDialog(Context context) { super(context); } public CustomDialog(Context context, int theme) { super(context, theme); } // 主要是重写Bulder内部类 public static class Builder { private Context context; private String title; private String message; private String positiveButtonText; private String negativeButtonText; private View contentView; private DialogInterface.OnClickListener positiveButtonClickListener; private DialogInterface.OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } public Builder setMessage(String message) { this.message = message; return this; } /** * 设置的内容 * * @param message * @return */ public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } /** * 设置标题 * * @param title * @return */ public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } /** * 设置标题 * * @param title * @return */ public Builder setTitle(String title) { this.title = title; return this; } public Builder setContentView(View v) { this.contentView = v; return this; } /** * 设置确定按钮 * * @param positiveButtonText * @param listener * @return */ public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } // 这是自定义弹出框的关键 public CustomDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final CustomDialog dialog = new CustomDialog(context, R.style.Dialog); View layout = inflater.inflate(R.layout.dialog, null); dialog.addContentView(layout, new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); // 设置标题 ((TextView) layout.findViewById(R.id.title)).setText(title); // 设置确认按钮 if (positiveButtonText != null) { ((Button) layout.findViewById(R.id.positiveButton)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((Button) layout.findViewById(R.id.positiveButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { //如果没有确认按钮则隐藏 layout.findViewById(R.id.positiveButton).setVisibility( View.GONE); } // 设置取消按钮 if (negativeButtonText != null) { ((Button) layout.findViewById(R.id.negativeButton)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((Button) layout.findViewById(R.id.negativeButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { //如果没有取消按钮则隐藏 layout.findViewById(R.id.negativeButton).setVisibility( View.GONE); } // 设置消息 if (message != null) { ((TextView) layout.findViewById(R.id.message)).setText(message); } else if (contentView != null) { ((LinearLayout) layout.findViewById(R.id.content)) .removeAllViews(); ((LinearLayout) layout.findViewById(R.id.content)).addView( contentView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } // 把设置好的View加载到弹出框 dialog.setContentView(layout); return dialog; } } }
2.弹出框所使用的布局文件
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="false" android:orientation="vertical" android:padding="20.0dip" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/bg_bombbox" android:orientation="vertical" > <TextView android:id="@+id/title" style="@style/text_18_ffffff" android:layout_width="fill_parent" android:layout_height="40.0dip" android:gravity="center" android:text="@string/title_alert" android:visibility="visible" /> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" > <TextView android:id="@+id/message" style="@style/text_16_666666" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left|center" android:lineSpacingMultiplier="1.5" android:minHeight="120.0dip" android:paddingBottom="15.0dip" android:paddingLeft="20.0dip" android:paddingRight="20.0dip" android:paddingTop="15.0dip" /> </LinearLayout> <!-- 设置分割线 --> <View android:layout_width="fill_parent" android:layout_height="1.0px" android:background="#8CC34B" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="60.0dip" android:layout_gravity="bottom" android:background="@drawable/dialog_bottom_bg" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/positiveButton" style="@style/text_15_ffffff_sdw" android:layout_width="114.0dip" android:layout_height="40.0dip" android:background="@drawable/btn_ok_selector" android:gravity="center" android:text="@string/ok" /> <Button android:id="@+id/negativeButton" style="@style/text_15_666666_sdw" android:layout_width="114.0dip" android:layout_height="40.0dip" android:layout_marginLeft="20.0dip" android:background="@drawable/btn_cancel_selector" android:gravity="center" android:text="@string/cancel" /> </LinearLayout> </LinearLayout> </FrameLayout>
3.activity内容
package com.test.activity; import android.app.Activity; import android.content.DialogInterface; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.Window; import com.test.R; import com.test.widget.CustomDialog; public class DialogActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.dialogmain); // 自定义标题 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void showAlertDialog(View view) { CustomDialog.Builder builder = new CustomDialog.Builder(this); builder.setMessage("这个就是自定义的提示框"); builder.setTitle("提示"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // 隐藏dialog dialog.dismiss(); System.out.println("------------------点击确定----------------"); } }); builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // 隐藏dialog dialog.dismiss(); System.out.println("------------------点击取消----------------"); } }); builder.create().show(); } }
使用替换layout布局文件的方法
页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 顶部椭园边缘 --> <ImageView android:layout_width="400dp" android:layout_height="22dp" android:src="@drawable/dialog_top" > </ImageView> <!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 --> <LinearLayout android:layout_width="400dp" android:layout_height="wrap_content" android:background="@drawable/dialog_center" android:orientation="vertical" > <TextView android:textColor="#000000" android:textSize="35dp" android:gravity="center" android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="20dp" android:layout_marginRight="10dp" android:id="@+id/message" android:layout_marginBottom="10dp" android:layout_marginTop="20dp" android:textColor="#000000" android:textSize="25dp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- 在LinearLayout中加按钮 --> <LinearLayout android:id="@+id/buttonLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:orientation="horizontal" > </LinearLayout> </LinearLayout> <!-- 底部椭园边缘 --> <ImageView android:layout_marginTop="-2dp" android:layout_width="400dp" android:layout_height="22dp" android:src="@drawable/dialog_bottom" > </ImageView> </LinearLayout>
selector
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/dialog_button_pressed" /> <item android:state_focused="true" android:drawable="@drawable/dialog_button_pressed" /> <item android:drawable="@drawable/dialog_button" /> </selector>
public class AlertDialog { Context context; android.app.AlertDialog ad; TextView titleView; TextView messageView; LinearLayout buttonLayout; public AlertDialog(Context context) { // TODO Auto-generated constructor stub this.context=context; ad=new android.app.AlertDialog.Builder(context).create(); ad.show(); //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局 Window window = ad.getWindow(); window.setContentView(R.layout.alertdialog); titleView=(TextView)window.findViewById(R.id.title); messageView=(TextView)window.findViewById(R.id.message); buttonLayout=(LinearLayout)window.findViewById(R.id.buttonLayout); } public void setTitle(int resId) { titleView.setText(resId); } public void setTitle(String title) { titleView.setText(title); } public void setMessage(int resId) { messageView.setText(resId); } public void setMessage(String message) { messageView.setText(message); } /** * 设置按钮 * @param text * @param listener */ public void setPositiveButton(String text,final View.OnClickListener listener) { Button button=new Button(context); LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); button.setBackgroundResource(R.drawable.alertdialog_button); button.setText(text); button.setTextColor(Color.WHITE); button.setTextSize(20); button.setOnClickListener(listener); buttonLayout.addView(button); } /** * 设置按钮 * @param text * @param listener */ public void setNegativeButton(String text,final View.OnClickListener listener) { Button button=new Button(context); LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); button.setBackgroundResource(R.drawable.alertdialog_button); button.setText(text); button.setTextColor(Color.WHITE); button.setTextSize(20); button.setOnClickListener(listener); if(buttonLayout.getChildCount()>0) { params.setMargins(20, 0, 0, 0); button.setLayoutParams(params); buttonLayout.addView(button, 1); }else{ button.setLayoutParams(params); buttonLayout.addView(button); } } /** * 关闭对话框 */ public void dismiss() { ad.dismiss(); } }
调用方法
final AlertDialog ad=new AlertDialog(Test.this); ad.setTitle("标题"); ad.setMessage("内容sdfsafdasf内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容"); ad.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ad.dismiss(); Toast.makeText(Test.this, "被点到确定", Toast.LENGTH_LONG).show(); } }); ad.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ad.dismiss(); Toast.makeText(Test.this, "被点到取消", Toast.LENGTH_LONG).show(); } });
转自:http://my.oschina.net/u/1036767/blog/266318?p=1
发表评论
文章已被作者锁定,不允许评论。
-
android BaseActivity与BaseFragment的封装
2016-05-19 15:06 9611 概述 多模块Activity+多Fragment 是开发 ... -
android ScrollView常用属性
2016-05-09 09:17 664android:scrollbars 设置滚动条显示。none ... -
android RecyclerView使用及详解
2016-05-04 10:35 28571.前言 话说RecyclerView已经面市很久,也在很多 ... -
android DialogFragment 创建对话框(官方推荐)
2016-04-29 14:22 8641、 概述 DialogFragment在android 3. ... -
android Intent Flags及Task相关属性
2016-04-26 09:15 572task是一个具有栈结构的容器,可以放置多个Activity实 ... -
android SQLite具体实例应用详解(SQLiteOpenHelper)
2016-04-18 19:24 774上次我向大家介绍了SQLite的基本信息和使用过程,相信朋友们 ... -
android SQLite3常用命令&语法
2016-04-18 19:01 573sqlite数据库只用一个文件就ok,小巧方便,所以是一个非常 ... -
android View绘制相关问题总结
2016-03-28 19:27 6441.View的绘制流程分几步 ... -
android Handler Looper MessageQueue机制的原理
2016-03-22 21:09 729andriod提供了Handler和Looper来满足线程间的 ... -
android Bitmap,BitmapFactory类图像处理
2016-03-16 16:28 2329BitMap代表一张位图,扩展名可以是.bmp或者.dib。 ... -
android 三类菜单(Option) (Context) (SubMenu)学习
2016-03-14 15:23 878在Android系统中,菜单可以分为三类:选项菜单(Optio ... -
android Sqlite数据库对象模型ORMLite框架学习
2016-03-11 15:24 675在Android项目中或多或少的都会使用数据库,为了提高我们的 ... -
android AsyncTask<参数,进度值类型,返回类型>学习
2016-03-10 14:45 1143AsyncTask和Handler对比 1 ... -
android 远程图片获取和本地缓存
2016-03-10 14:32 725概述 对于客户端——服 ... -
android onSaveInstanceState和onRestoreInstanceState学习
2016-03-10 10:08 753Android中的activity通过onSaveInst ... -
android Fragment生命周期和回退栈学习以及Fragment之间通信
2016-03-10 09:31 1495会涉及到Fragment如何产生,什么是Fragment,Fr ... -
android LruCache内存缓存学习(重写sizeOf方法)
2016-03-09 19:25 1020什么是缓存? 缓存技术原理就是把用户访问的所有对象看作一个全 ... -
android ListView中的convertView缓存及使用
2016-03-09 19:12 1669一、复用convertView 首先讲下ListView的原 ... -
android 从资源角度谈Android代码内存优化
2016-03-09 18:54 672开发人员如果在进行代码编写之前就有内存泄露方面的基础知识,那么 ... -
android startActivityForResult和setResult详情及Demo
2016-03-09 08:56 1010startActivityForResult与startAct ...
相关推荐
android自定义透明对话框,透明提示框,自定义Dialog.zip
Android界面之几种常用的自定义Dialog.zip
android 自定义 dialog android 自定义 dialog android 自定义 dialog android 自定义 dialog
一个自定义的dialog,7种动画效果。弹出你想要的自定义窗口!
jquery.dialog.js 是jquery 最经典的弹出框插件,简单易用,亲测可用,附案例代码
自定义dialog.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
自定义dialog弹出框,通过shape和seleter自定义出自己想要的样式。还有弹出的动画,从不同方向弹出和效果。
android自定义dialog
很好android自定义dialog加载转圈等待,适合初学,透明+正常两种主题,屏蔽返回键(自己修改参数,主题也是)
Android自定义Dialog--DanmakuFlame弹幕发送框界面的实现
一个可以直接使用的自定义AlertDialog,封装的很完整的一个Demo,可以灵活使用,里面提示图标还额外添加了动画效果,希望可以帮到大家。
Android自定义dialog,需要的继续带走
android 自定义对话框.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android-styled-dialogs 可自定义样式的dialog.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android自定义全屏dialog,可以全屏显示图片。
Android自定义dialogDemo
自己整理的自定义控件方法,看了你就会做自定义Dialog和Toast了,没有多余代码,很简洁。背景、按钮都能自定义
一个简单的Android自定义Dialog.
自定义构建者dialog.rar