`
endual
  • 浏览: 3546056 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

android 自定义对话框

 
阅读更多
很多时候,我们需要一个自定义的对话框,这个对话框最好是由一个layout.xml的自己设计,
不是由代码敲出来的,这里记录下简单的自定义的对话框。
首先创建一个android工程:
在MainActivity中

package endual.main;

import android.app.Activity;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.os.Bundle;

public class MianActivity extends Activity implements
android.view.View.OnClickListener {
Button btn1 = null;
Button btn2 = null;
Button btn3 = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.b1);
btn2 = (Button) findViewById(R.id.b2);
btn3 = (Button) findViewById(R.id.b3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
break;
case R.id.b2:
case R.id.b3:
new MyDialogs(this).setDisplay();
break;
default:

}
}

// 自定义对话框
class MyDialogs extends Dialog implements android.view.View.OnClickListener {
private Button b1;
private Window window = null;

public MyDialogs(Context context) {
super(context);
}

public void setDisplay() {
setContentView(R.layout.dialog);// 设置对话框的布局
b1 = (Button) findViewById(R.id.clo);
b1.setOnClickListener(this);
setProperty();
setTitle("自定义对话框");// 设定对话框的标题
show();// 显示对话框
}

// 要显示这个对话框,只要创建该类对象.然后调用该函数即可.
public void setProperty() {
window = getWindow();//    得到对话框的窗口.
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;// 这两句设置了对话框的位置.0为中间
wl.y = 180;
wl.alpha = 0.6f;// 这句设置了对话框的透明度
wl.gravity = Gravity.BOTTOM;
window.setAttributes(wl);
}

@Override
public void onClick(View v) {
dismiss();// 取消
}
}
}

创建两个布局文件
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/b1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />

    <Button
        android:id="@+id/b2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />

    <Button
        android:id="@+id/b3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

------------------------------------------------------------------
dialog.xml 这个是由我们自己定义的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android1:layout_width="400px"
        android1:layout_height="81dp"
        android1:gravity="center"
        android1:text="xxxxxxxxxxxxxxxxxxxxxxxx/nnxxxxxxxxxxxxxxxxxx" />

    <RadioGroup
        android1:id="@+id/radioGroup1"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content" >

        <RadioButton
            android1:id="@+id/radio0"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:checked="true"
            android1:text="RadioButton" />

        <RadioButton
            android1:id="@+id/radio1"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:text="RadioButton" />

        <RadioButton
            android1:id="@+id/radio2"
            android1:layout_width="wrap_content"
            android1:layout_height="wrap_content"
            android1:text="RadioButton" />
    </RadioGroup>

    <Button
        android:id="@+id/clo"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

------------------------------------------
好了,简单的自定义的dialog就创建好,网上还有许多类似的题材,这个demo也是根据人家的资料修改的。






分享到:
评论

相关推荐

    android自定义对话框、dialog

    总的来说,创建Android自定义对话框需要理解DialogFragment或AlertDialog.Builder的工作原理,以及如何将它们与自定义布局相结合。通过`defineDialog`这样的示例,开发者可以学习到如何从头开始构建一个具有特定功能...

    安卓Android源码——android 自定义对话框.rar

    本资源“安卓Android源码——android 自定义对话框.rar”显然包含了关于如何在Android平台上创建和定制对话框的源代码示例。通过这个压缩包,我们可以学习到如何摆脱系统默认样式,设计出更符合应用风格的对话框。 ...

    Android自定义对话框实现QQ退出界面

    在Android开发中,自定义对话框...通过以上步骤,我们成功地实现了Android自定义对话框,模拟了QQ退出界面的效果。在实际应用中,可以根据项目需求对这个对话框进行进一步的定制和优化,以提供更加个性化的用户体验。

    Android自定义对话框Dialog(界面美化)

    总之,Android自定义对话框Dialog的界面美化是一个涉及布局设计、样式定制、事件处理等多个方面的过程。通过深入理解并实践这些技术,开发者可以创造出更具吸引力和个性化的Dialog,从而提高用户对应用的满意度。

    Android应用源码之android 自定义对话框.zip

    在Android开发中,自定义对话框(Custom Dialog)是一个重要的组件,它允许开发者根据应用程序的UI风格和功能需求创建独特且交互性强的弹出界面。本压缩包中的资源提供了关于如何在Android应用中实现自定义对话框的...

    Android自定义对话框

    一般自定义对话框有三种办法: 1、重写Dialog来实现。 2、获取Dialog的Window对象实现。 3、使用WindowManager来实现。 4、使用DialogTheme来实现。 详细请看:...

    Android例子源码自定义对话框

    在Android开发中,自定义对话框(Dialog)是常见的需求,它可以为用户提供更丰富的交互体验。自定义对话框可以按照应用程序的设计风格进行定制,包括布局、颜色、按钮样式等,从而提升应用的用户体验。本篇将围绕...

    android 自定义对话框.zip源码资源下载

    下面将详细介绍Android自定义对话框的相关知识点。 首先,Android中的对话框通常是通过`Dialog`类或其子类如`AlertDialog`来创建的。`Dialog`继承自`Activity`,而`AlertDialog`则是`Dialog`的一个更具体的实现,常...

    Android自定义对话框(代码)

    在Android开发中,自定义对话框(Custom Dialog)是一种常用的技术,它允许开发者根据应用的UI风格和功能需求创建个性化的对话框。本教程将详细讲解如何在Android中实现自定义对话框,并提供代码示例。 一、Android...

    android自定义对话框加动画

    在Android开发中,自定义对话框...综上所述,Android自定义对话框结合动画的应用不仅能够丰富用户体验,还能增强应用的美观度。通过合理的布局设计和动画设置,开发者可以创造出功能强大且具有吸引力的对话框组件。

    安卓Android源码——android 自定义对话框.zip

    本资源"安卓Android源码——android 自定义对话框.zip"包含了实现自定义对话框的源代码,这对于理解和实践Android应用的界面定制具有重要意义。 首先,我们要理解Android系统提供的默认对话框类`AlertDialog`和`...

    Android自定义对话框的使用

    这篇博客文章“Android自定义对话框的使用”深入探讨了如何在Android项目中创建和使用自定义对话框。 首先,我们了解自定义对话框的基本概念。对话框(Dialog)是Android中的一个窗口,它浮于应用程序之上,用于向...

    android 自定义对话框.zip

    在Android应用开发中,自定义...通过这个示例项目,你可以深入理解Android自定义对话框的实现原理,以及如何在实际项目中应用。同时,也可以借此机会提高对Android源码阅读和分析的能力,进一步提升你的开发技能。

    android 自定义对话框.zip项目安卓应用源码下载

    通过研究这个项目,你可以更好地理解和掌握Android自定义对话框的实践技巧,从而在自己的项目中灵活运用。无论是学生的学习、个人的技能提升还是公司的项目开发,这个源码都是一个很好的参考资料。

    安卓开发-android 自定义对话框.zip

    在Android开发中,自定义对话框(Custom Dialog)是一个重要的组件,它允许开发者根据应用程序的UI风格和功能需求创建独特且具有交互性的弹出窗口。本资料“安卓开发-android 自定义对话框.zip”可能包含一系列关于...

    Android中自定义对话框的实现

    在Android开发中,自定义对话框(Custom Dialog)是一种常见的用户界面组件,它允许开发者根据应用的风格和需求创建独特的提示或交互界面。本篇将深入探讨如何在Android中实现自定义对话框,并通过示例代码`MyDialog...

Global site tag (gtag.js) - Google Analytics