Android UI组件进阶(2)——仿Windows对话框

来源:互联网 发布:软件接口怎么费 编辑:程序博客网 时间:2024/05/23 17:31

在开始本章前先祝大家中秋节快乐哈,相信很多上班的朋友都是放三天假的哈!大笑

有时间的话回家陪陪父母吧!树欲静而风不止,子欲养而亲不待!岁月不饶人!

好了,道理和祝福语就说到这里了,今天给大家准备的是模仿Windows风格对话框!


效果图:

相信大部分的AlertDialog都是下面这个样子的:



今天给大家讲解的对话框是下面这样的:



对比两种对话框,站在用户的角度,相信你更加钟情于第二种颜色鲜明的对话框

好了下面就开始讲解如何制作模仿windows风格的对话框吧!




代码的逻辑流程:

其实就是自定义对话框的布局,然后加载以及完成相应的事件处理而已!




核心代码解析:

①按钮点击效果:btnexit_selctor.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:state_pressed="false" android:drawable="@drawable/btnexit"/>  
  4.     <item android:state_pressed="true" android:drawable="@drawable/btnexit_s"/>  
  5. </selector>  



②对话框的布局文件:dialog_win.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/RelativeLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RelativeLayout  
  9.         android:id="@+id/titlelayout"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:background="#53CC66"  
  15.         android:padding="5dp" >  
  16.   
  17.         <TextView  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:text="提示信息"  
  21.             android:textColor="#ffffff"  
  22.             android:textSize="15sp" />  
  23.         <ImageButton  
  24.             android:id="@+id/btncancle"   
  25.             android:layout_width="30dp"  
  26.             android:layout_height="30dp"  
  27.             android:background="@drawable/btnexit_selctor"  
  28.             android:layout_alignParentRight="true"      
  29.         />  
  30.           
  31.     </RelativeLayout>  
  32.       
  33.      
  34.   
  35.     <LinearLayout  
  36.         android:id="@+id/detaillayout"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_alignParentLeft="true"  
  40.         android:layout_below="@+id/titlelayout"  
  41.         android:layout_marginLeft="30dp"  
  42.         android:layout_marginTop="22dp"  
  43.         android:orientation="vertical" >  
  44.   
  45.         <TextView  
  46.             android:layout_width="wrap_content"  
  47.             android:layout_height="wrap_content"  
  48.             android:layout_marginLeft="10dp"  
  49.             android:layout_marginTop="10dp"  
  50.             android:text="仿Windows对话框Demo"  
  51.             android:textColor="#04AEDA"  
  52.             android:textSize="18sp" />  
  53.   
  54.         <TextView  
  55.             android:layout_width="wrap_content"  
  56.             android:layout_height="wrap_content"  
  57.             android:layout_marginLeft="10dp"  
  58.             android:layout_marginTop="10dp"  
  59.             android:text="作者:Coder-pig"  
  60.             android:textColor="#04AEDA"  
  61.             android:textSize="18sp" />  
  62.     </LinearLayout>  
  63.       
  64.      <LinearLayout  
  65.         android:layout_marginTop="30dp"  
  66.         android:layout_width="match_parent"  
  67.         android:layout_height="wrap_content"  
  68.         android:orientation="horizontal"  
  69.         android:layout_below="@+id/detaillayout"  
  70.     >  
  71.           
  72.      <Button  
  73.             android:id="@+id/btnblog"  
  74.             android:layout_width="match_parent"  
  75.             android:layout_height="40dp"  
  76.             android:background="@drawable/btnpress_selctor"  
  77.             android:text="访问博客"  
  78.             android:layout_margin="5dp"  
  79.             android:layout_weight="1"  
  80.             android:textColor="#ffffff"  
  81.             android:textSize="20sp"    
  82.       />  
  83.         
  84.      <Button  
  85.             android:id="@+id/btnclose"   
  86.             android:layout_width="match_parent"  
  87.             android:layout_height="40dp"  
  88.             android:background="@drawable/btnpress_selctor"  
  89.             android:text="关闭"  
  90.             android:layout_margin="5dp"  
  91.             android:layout_weight="1"   
  92.             android:textColor="#ffffff"  
  93.             android:textSize="20sp"   
  94.       />  
  95.           
  96.     </LinearLayout>  
  97.       
  98. </RelativeLayout>  



③MainActivity.java

[java] view plaincopyprint?
  1. package com.jay.example.windowsdialogdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10.   
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.ImageButton;  
  16. import android.widget.Toast;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     private Button btnshow;  
  21.     private View dialogView;  
  22.       
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.           
  28.         btnshow = (Button) findViewById(R.id.btnshow);  
  29.         btnshow.setOnClickListener(new OnClickListener() {    
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                   
  33.                 Builder builder = myBuilder(MainActivity.this);  
  34.                 final AlertDialog dialog = builder.show();  
  35.                 //设置点击屏幕外侧,对话框不消失,不设置的话点外面对话框就会消失  
  36.                 dialog.setCanceledOnTouchOutside(false);  
  37.                 //接着要监听对话框中的三个按钮:  
  38.                 //①关闭对话框的按钮:  
  39.                 ImageButton imgcancle = (ImageButton) dialogView.findViewById(R.id.btncancle);  
  40.                 imgcancle.setOnClickListener(new OnClickListener() {      
  41.                     @Override  
  42.                     public void onClick(View v) {  
  43.                         dialog.dismiss();  
  44.                     }  
  45.                 });  
  46.                   
  47.                 //①链接到博客的按钮  
  48.                 Button btnblob = (Button) dialogView.findViewById(R.id.btnblog);  
  49.                 btnblob.setOnClickListener(new OnClickListener() {    
  50.                     @Override  
  51.                     public void onClick(View v) {  
  52.                         Toast.makeText(getApplicationContext(), "访问博客", Toast.LENGTH_SHORT).show();  
  53.                         Uri uri = Uri.parse("http://blog.csdn.net/coder_pig");  
  54.                         Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
  55.                         startActivity(intent);  
  56.                         dialog.dismiss();     
  57.                     }  
  58.                 });  
  59.                   
  60.                 //③点击退出的按钮:  
  61.                 Button btnclose = (Button) dialogView.findViewById(R.id.btnclose);  
  62.                 btnclose.setOnClickListener(new OnClickListener() {  
  63.   
  64.                     public void onClick(View v) {  
  65.                         dialog.dismiss();  
  66.                     }  
  67.                 });  
  68.             }  
  69.   
  70.               
  71.             //定义一个返回Builder对象的方法,其实这里是设置对话框显示view内容的方法  
  72.             private Builder myBuilder(Context context) {  
  73.                 final LayoutInflater inflater = MainActivity.this.getLayoutInflater();  
  74.                 AlertDialog.Builder builder = new AlertDialog.Builder(context);  
  75.                 dialogView = inflater.inflate(R.layout.dialog_win, null);  
  76.                 return builder.setView(dialogView);  
  77.             }     
  78.         });  
  79.     }  
  80.       
  81. }  



实例代码下载:

http://pan.baidu.com/s/1jGone5o


0 0
原创粉丝点击