首页 > 其他 > 详细

AlertDialog

时间:2015-10-01 21:40:09      阅读:286      评论:0      收藏:0      [点我收藏+]

技术分享

1)更改AlertDialog窗口大小的方法: 
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = 200;
params.height = 200 ;
dialog.getWindow().setAttributes(params);
2)去除边框
AlertDialog.setView(view,0,0,0,0);

伪装dialog大小的改变
 1. 创建一个样式文件到你的工程,保存在在res/values/styles.xml,这里文件名不能随便修改,内容为,注意保存时使用UTF-8编码。
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="Theme.Android123" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/bg</item>
 </style>
</resources>
 
 2. 上面我们定义的主题风格为Theme.Android123,父风格仍然从Theme.Dialog实现,但我们自定义了背景,
 位置在drawable/bg中,这里我们创建一个bg.xml文件放到res/drawable文件夹中,bg.xml的内容为
 
  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="15dip" android:top="15dip"
android:right="15dip" android:bottom="15dip" />
<stroke android:width="3dip" color="#000000" />
<corners android:radius="5dp" />
<solid android:color="#ffffff" />
  </shape>
 
 里面我们定义了一个shape对象,实现背景drawable形状,
 其中padding代表距离边框,
 这里我们设置了左、上、右、下四个位置的间距。
 stroke可以制造出一些3D立体效果,
 corners是四个角,
 radisu属性可以设置半径,值越大越圆滑,根据运行效果你可以微调,
 最后soild是填充颜色,这里我们用了ffffff表示纯白。
 
  3. 最后在androidmanifest.xml中,在你的activity节点加一个 android:theme属性,值为@style/Theme.Android123 即可。
 
 
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 = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
//        p.width = (int) (d.getWidth() * 0.65); // 宽度设置为屏幕的0.65
//        dialogWindow.setAttributes(p);

        dialog.show();

    }
}

布局文件:

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:background="#00FF00"
    android:orientation="vertical" >

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

</LinearLayout>

dialog_layout.xml

<?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>

 

 

 

自定义dialog的布局样式

在网上找了很多关于dialog的自定义样式的问题,还有很多人写得比较复杂,需要改动style什么的,或者是自定义dialog搞得很复杂,我最后还是找到了方法来实现。参考原文地址:http://jy0329.blog.163.com/blog/static/14746600220132204035120/

 

下面是我的dialog布局xml文件:

<</span>RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:background="@android:color/transparent"

    android:layout_margin="50dp">

   

    <</span>RelativeLayout

        android:id="@+id/rl_dialog_content"

        android:layout_width="fill_parent"

        android:layout_height="200dp"

        android:background="@drawable/alertdialog_bg">

        <</span>TextView

            android:id="@+id/dialog_text"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="现在就打电话给客服:arjinmc"   

            android:layout_marginTop="50dp"    

            android:layout_marginLeft="30dp"

            android:layout_marginRight="30dp"

            android:maxLines="5"

            android:gravity="center"

            />       

           

        <</span>LinearLayout

            android:id="@+id/ll_buttons"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal"

            android:layout_alignParentBottom="true"

            android:layout_margin="30dp">

            

           

            <</span>Button

                 android:id="@+id/dialog_cancel"

                 android:text="@string/alert_cancel"

                 android:background="@drawable/btn_long_white"

                 style="@style/dialog_button"

                 android:layout_weight="1"

                 android:textColor="@color/tabs_font"

                 />

            <</span>Button

                 android:id="@+id/dialog_ok"

                 android:text="@string/alert_ok"

                 android:background="@drawable/btn_long_red"

                 style="@style/dialog_button"

                 android:layout_weight="1"

                 android:layout_marginLeft="5dp"/>

           

        </</span>LinearLayout>

    </</span>RelativeLayout>

    <</span>ImageButton

        android:id="@+id/dialog_close"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/alertdialog_close"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="20dp"

       />  

   

 

</</span>RelativeLayout>

效果如图:

 

 

在代码中需要这项写就可以了:

//布局文件转换为view对象

         LayoutInflater inflaterDl = LayoutInflater.from(this);

         RelativeLayout layout = (RelativeLayout)inflaterDl.inflate(R.layout.layout_dialog, null );

        

         //对话框

         final Dialog dialog = newAlertDialog.Builder(SettingActivity.this).create();

         dialog.show();

         dialog.getWindow().setContentView(layout);

        

        

         //取消按钮

         Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);

         btnCancel.setOnClickListener(new OnClickListener() {

          

           @Override

           public void onClick(View v) {

              Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();           

           }

         });

        

        

         //确定按钮

         Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);

         btnOK.setOnClickListener(new OnClickListener() {

          

           @Override

           public void onClick(View v) {

              Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();           

           }

         });

        

        

         //关闭按钮

         ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);

         btnClose.setOnClickListener(new OnClickListener() {

          

           @Override

           public void onClick(View v) {

              dialog.dismiss();          

           }

         });

 

 

非常easy!自己动手吧。

AlertDialog

原文:http://www.cnblogs.com/butterfly-clover/p/4851582.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!