码迷,mamicode.com
首页 > 移动开发 > 详细

Android-自定义dialog

时间:2014-04-30 23:16:35      阅读:853      评论:0      收藏:0      [点我收藏+]

标签:android   com   http   blog   style   class   div   img   code   java   c   

自定义Dialog位置和大小

 2835人阅读 评论(0) 收藏 举报
 

目录(?)[+]

 
mamicode.com,码迷

mamicode.com,码迷
package angel.devil;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

public class DialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog dialog = new Dialog(this);

// setContentView可以设置为一个View也可以简单地指定资源ID
// LayoutInflater
// li=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
// View v=li.inflate(R.layout.dialog_layout, null);
// dialog.setContentView(v);
dialog.setContentView(R.layout.dialog_layout);

dialog.setTitle("Custom Dialog");

/*
* 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置,
* 可以直接调用getWindow(),表示获得这个Activity的Window
* 对象,这样这可以以同样的方式改变这个Activity的属性.
*/
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);

/*
* lp.x与lp.y表示相对于原始位置的偏移.
* 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
* 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
* 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
* 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
* 当参数值包含Gravity.CENTER_HORIZONTAL时
* ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
* 当参数值包含Gravity.CENTER_VERTICAL时
* ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
* gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
* Gravity.CENTER_VERTICAL.
*
* 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
* 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了,
* Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
*/
lp.x = 100; // 新位置X坐标
lp.y = 100; // 新位置Y坐标
lp.width = 300; // 宽度
lp.height = 300; // 高度
lp.alpha = 0.7f; // 透明度

// 当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes
// dialog.onWindowAttributesChanged(lp);
dialogWindow.setAttributes(lp);

/*
* 将对话框的大小按屏幕大小的百分比设置
*/
// WindowManager m = getWindowManager();
// Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
// WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
// p.width = (int) (d.getWidth() * 0.65); // 宽度设置为屏幕的0.95
// dialogWindow.setAttributes(p);

dialog.show();

}
}
mamicode.com,码迷
mamicode.com,码迷

 

布局文件:

main.xml

mamicode.com,码迷
mamicode.com,码迷
<?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:background
="#00FF00"
android:orientation
="vertical" >

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

</LinearLayout>
mamicode.com,码迷
mamicode.com,码迷

dialog_layout.xml

mamicode.com,码迷
mamicode.com,码迷
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id
="@+id/layout_root"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:orientation
="horizontal"
android:padding
="10dp" >

<ImageView
android:id="@+id/image"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_marginRight
="10dp"
android:src
="@drawable/ic_launcher" />

<TextView
android:id="@+id/text"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="A Dialog"
android:textColor
="#FFF" />

</LinearLayout>
mamicode.com,码迷
 
 
   

android中设置AlertDialog的大小

分类: android 345人阅读 评论(0) 收藏 举报
[java] view plaincopy
 
 
 
  1. AlertDialog dialog = builder.setTitle("消息列表")  
  2.                     .setView(layout)  
  3.                     .create();  
  4. dialog.show();  
  5. //设置窗口的大小  
  6. dialog.getWindow().setLayout(300200);  


dialog.show();一定要放在dialog.getWindow().setLayout(300, 200);的前面,否则不起作用。

网上有一种方法是
[java] view plaincopy
 
 
 
  1. WindowManager.LayoutParams params = dialog.getWindow().getAttributes();  
  2. params.width = 300;  
  3. params.height = 200;  
  4. dialog.getWindow().setAttributes(params);  
但是dialog.getWindow().setLayout(300, 200);实际上封装了这个方法,setLayout()的源代码如下:
[java] view plaincopy
 
 
 
  1. final WindowManager.LayoutParams attrs = getAttributes();  
  2. attrs.width = width;  
  3. attrs.height = height;  
  4. if (mCallback != null) {  
  5.     mCallback.onWindowAttributesChanged(attrs);  
  6. }  

所以这两个方法的作用本质上是一样的,都是为AlertDialog设置大小
 
 
  1. WindowManager m = getWindowManager();  
  2. Display d = m.getDefaultDisplay();  //为获取屏幕宽、高  
  3.   
  4. LayoutParams p = getWindow().getAttributes();  //获取对话框当前的参数值  
  5. p.height = (int) (d.getHeight() * 0.6);   //高度设置为屏幕的0.6  
  6. p.width = (int) (d.getWidth() * 0.95);    //宽度设置为屏幕的0.95  
  7.   
  8. getWindow().setAttributes(p);     //设置生效  
 
 
 

 

public class Test_dialog extends Dialog{

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.dialog_testinginput);

//设定大小

LayoutParams params = getWindow().getAttributes();   

        params.height = LayoutParams.FILL_PARENT;    

        params.width = LayoutParams.FILL_PARENT;   

        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 

//----------------------------------

}

}

=================================================================
 
 
楼主
 
mamicode.com,码迷 发表于 2011-12-20 19:39:45 | 查看: 1608| 回复: 4
先介绍一些关于AlertDialog的基本知识: 

    一、AlertDialog简介:AlertDialog的构造方法被声明为protected,所以不能直接使用new关键字来创建AlertDialog类的对象实例。要想创建AlertDialog对话框,需要使用Builder类,该类是AlertDialog类中定义的一个内嵌类。因此必须创建AlertDialog.Builder类的对象实例,然后再调用show()来显示对话框。


    二、使用AlertDialog创建对话框的种类:


        1. 最多带3个按钮的对话框:setPositiveButton(...)--确认、setNegativeButton(...)--取消、setNeutralButton(...)--忽略


        2.简单列表对话框:通过AlertDialog.Builder类的setItems(...)方法可以创建简单的列表对话框。其实,这种类型的对话框相当于将ListView组件放在对话框上,然后再在ListView中添加若干简单的文本。


        3.单选列表对话框:通过AlertDialog.Builder类的setSingleChoiceItems(...)来创建。目前支持4种数据源(数组资源、数据集、字符串数组、ListAdapter)


        4.多选列表对话框:通过AlertDialog.Builder类的setMultiChoiceItems(...)创建。目前支持3种数据源(数组资源、数据集、字符串数组)


        5.水平进度或圆形对话框(默认是:圆形):该类型的对话框是通过ProgressDialog来实现,该类是AlertDialog的子类,它不需要用create()方法来返回实例对象,只需要new即可。


           ProgressDialog.STYLE_HORIZONTAL //水平进度样式


           ProgressDialog.STYLE_SPINNER    //圆形样式


        6.自定义对话框:直接使用XML布局文件或以编写JAVA代码方式来创建视图,并将这些视图对象添加到对话框中去。


        7.使用Activity托管对话框:Activity类中也提供了创建对话框的方式,有个onCreateDialog(int id)的方法,其返回类型是Dialog,通过是当调用Activity类的showDialog(int id)方法时,系统会调用该方法来返回一个Dialog对象。showDialog和onCreateDialog都有一个int类型的id参数,该参数值将传递给onCreateDialog方法。因此,我们可以利用不同的id创建多个对话框。


         ***注意***:对于表示某一个对话框的ID,系统只在第1次调用showDialog方法时调用onCreateDialog方法。在第1次创建Dialog对象时系统会将该对象保存在Activity的缓存里,相当于一个Map对象,对话框的ID作为Map的Key,而Dialog对象作为Map的Value。下次再调用时,会先根据这个ID从Map中获得第1次创建的Dialog对象。除非该ID已经被删除。


        8.悬浮对话框和触摸任何位置都可以关闭的对话框:


        1).悬浮对话框:android:theme="@android:style/Theme.Dialog";对于该类型的对话框,触摸屏幕任何位置都会触发Activity的OnTouchEvent事件。


         2).触摸任何位置都可以关闭的对话框:首先必须要继承AlertDialog类,并重写OnTouchEvent事件。


第一种:

Java代码
  1. /** 
  2. * 自定义AlertDialog 
  3. * @author chenjianli 2011-05-10 
  4. */ 
  5. public void alert(){ 
  6.         WindowManager manager = getWindowManager(); 
  7.         Display display = manager.getDefaultDisplay(); 
  8.         int width = display.getWidth(); 
  9.         int height = display.getHeight(); 
  10.         LayoutInflater inflater = getLayoutInflater(); 
  11.         View view = inflater.inflate(R.layout.alert, null); 
  12.         TextView text = (TextView)view.findViewById(R.id.text); 
  13.         text.setText("自定义AlertDialog"); 
  14.         AlertDialog alert = new AlertDialog.Builder(this).create(); 
  15.         alert.show(); 
  16.         alert.getWindow().setLayout(width/2, height/4); 
  17.         alert.setTitle("测试"); 
  18.         alert.getWindow().setContentView(R.layout.alert); 
  19. }
复制代码
第二种: 
Java代码
  1. /** 
  2. * 自定义AlertDialog 
  3. * @author chenjianli 2011-05-10 
  4. */ 
  5. AlertDialog zidongbofangDialog = new AlertDialog.Builder(ManHuaActivity.this).create(); 
  6. zidongbofangDialog.show(); 
  7. zidongbofangDialog.getWindow().setGravity(Gravity.CENTER); 
  8. zidongbofangDialog.getWindow().setLayout( 
  9. android.view.WindowManager.LayoutParams.FILL_PARENT, 
  10. android.view.WindowManager.LayoutParams.WRAP_CONTENT); 
  11. zidongbofangDialog.getWindow().setContentView(R.layout.manhua_dialog_zidongbofang);
复制代码
第三种: 
/** 
* 自定义AlertDialog 

* @author chenjianli 2011-05-10 
*/ 
如果我们setView(),中的View是带EditText的,此时,我们必须在show()之前加上这么一句话,才可以在点击EditText时弹出键盘,否则将很杯具!键盘是弹不出来的。 
Java代码
  1. AlertDialog tiaozhuanDialog= new AlertDialog.Builder(ManHuaActivity.this).create(); 
  2. tiaozhuanDialog.setView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null)); 
  3. tiaozhuanDialog.show(); 
  4. tiaozhuanDialog.getWindow().setGravity(Gravity.CENTER); 
  5. tiaozhuanDialog.getWindow().setLayout( 
  6. android.view.WindowManager.LayoutParams.FILL_PARENT, 
  7. android.view.WindowManager.LayoutParams.WRAP_CONTENT); 
  8. tiaozhuanDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
复制代码
这里还有一个地方需要注意一下,如果我们在show这个AlertDialog之前,需要设置该AlertDialog显示的View中的EditText的内容,则我们应该这么去findViewById(): 
Java代码
  1. EditText editText = (EditText)tiaozhuanDialog.findViewById(R.id.myEditText); 
  2. editText.setText("Who are you ? I am android Developer ");
复制代码
否则会报ERROR/AndroidRuntime(1032): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child‘s parent first.错误!! 
mamicode.com,码迷

Android-自定义dialog,码迷,mamicode.com

Android-自定义dialog

标签:android   com   http   blog   style   class   div   img   code   java   c   

原文地址:http://www.cnblogs.com/CaptainLin/p/3701295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!