自定义 Dialog
2012-02-08 15:34
155 查看
转载自:http://www.iteye.com/topic/568409
Android
自定义 Dialog
[功能]
android 提供给我们的只有2种Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特点:1. 不是 Activity 2. 开销比 Activity 小得多
鉴于以上的优点 我们就有定制自己Dialog 的需求
[原理]
1. android 系统提供了一个class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先调用系统的"super.onCreate(savedInstanceState)" 其就会保证调用这个method.
2. 至于 Dialog 界面的定制 可以写一个xml 文件 然后 在 "void onCreate(Bundle)" 通过 "setContentView()" 来使之生效
3. Dialog 使用问题: 1. 弹出:show() 2. 取消:dismiss()
[代码]
1. 创建一个 Dialog .
2. Dialog 的使用:
3. Dialog 的界面定制.
Android
自定义 Dialog
[功能]
android 提供给我们的只有2种Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特点:1. 不是 Activity 2. 开销比 Activity 小得多
鉴于以上的优点 我们就有定制自己Dialog 的需求
[原理]
1. android 系统提供了一个class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先调用系统的"super.onCreate(savedInstanceState)" 其就会保证调用这个method.
2. 至于 Dialog 界面的定制 可以写一个xml 文件 然后 在 "void onCreate(Bundle)" 通过 "setContentView()" 来使之生效
3. Dialog 使用问题: 1. 弹出:show() 2. 取消:dismiss()
[代码]
1. 创建一个 Dialog .
public class CustomDialog extends Dialog { public CustomDialog(Context context) { super(context); // TODO Auto-generated constructor stub } protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.custom_dialog); setTitle("Custom Dialog"); TextView text = (TextView)findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView)findViewById(R.id.image); image.setImageResource(R.drawable.sepurple); Button buttonYes = (Button) findViewById(R.id.button_yes); buttonYes.setHeight(5); buttonYes.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub dismiss(); } }); Button buttonNo = (Button) findViewById(R.id.button_no); buttonNo.setSingleLine(true); buttonNo.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub dismiss(); } }); } //called when this dialog is dismissed protected void onStop() { Log.d("TAG","+++++++++++++++++++++++++++"); } }
2. Dialog 的使用:
public class CustomDialogUsage extends Activity { CustomDialog cd; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cd = new CustomDialog(this); Button buttonYes = (Button) findViewById(R.id.main_button); buttonYes.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub cd.show(); } }); } }
3. Dialog 的界面定制.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px" > <Button android:id="@+id/button_yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Yes " android:gravity="center" /> <Button android:id="@+id/button_no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" No " android:gravity="center" /> </LinearLayout> </LinearLayout> </LinearLayout>
相关文章推荐
- 用Dialog实现自定义对话框
- android 组件之Dialog(自定义)
- 自定义Dialog
- 自定义dialog包含一个显示图片的listview
- android 几种自定义 Dialog
- android 自定义Dialog(背景图等)
- 自定义Dialog
- Android FlycoDialog 简单实用的自定义Android弹窗对话框之Dialog篇
- 自定义Dialog对话框
- 简单好用的自定义Dialog(一)
- android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选的、自定义的对话框
- android--------自定义Dialog之信息提示
- 点击消失Toast - 自定义View dialog
- Android随记------Dialog自定义背景等样式
- 自定义Dialog对话框并实现从底部弹出的动画效果
- (转)Android:去除自定义dialog的title
- DialogFragment的使用及利用自定义interface点击事件并解决旋转屏幕点击事件失效问题
- Dialog和自定义Dialog以及日期对话框
- (转)自定义Dialog宽度占满屏幕
- 自定义带圆形头像的dialog弹出框