Android:自定义Material Design风格的Dialog

转载地址:http://blog.csdn.net/cassiepython/article/details/51227713




Android:自定义Material Design风格的Dialog


项目需要,所以来做一个Material Design风格的选择的对话框。

界面如下所示:



先来看看布局文件:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/dialog_rootView"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#55000000"  
  7.     android:padding="32dp" >  
  8.   
  9.     <RelativeLayout  
  10.         android:id="@+id/contentDialog"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_centerInParent="true"  
  14.         android:background="@drawable/dialog_background"  
  15.         android:padding="24dp" >  
  16.   
  17.         <TextView  
  18.             android:id="@+id/title"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_marginBottom="20dp"  
  22.             android:text="Title"  
  23.             android:textAppearance="?android:attr/textAppearanceLarge"  
  24.             android:textColor="#000" />  
  25.   
  26.         <LinearLayout  
  27.             android:layout_width="match_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:layout_below="@id/title"  
  30.             android:orientation="vertical"  
  31.             android:paddingBottom="20dp" >  
  32.   
  33.             <ListView  
  34.                 android:id="@+id/list_view"  
  35.                 android:layout_width="match_parent"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:footerDividersEnabled="true" >  
  38.             </ListView>  
  39.         </LinearLayout>  
  40.     </RelativeLayout>  
  41. </RelativeLayout></span>  

实现起来比较简单,我们看图就知道 ,我们需要一个title,也就是标题栏,所以布局中有一个TextView来承载标题的文本内容,下面就是一个listview,用于加载我们的每一项。
所以我们在代码中主要就是要处理后这两个控件。

自定义Dialog代码:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">package com.testdialog.testmydialog;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.Window;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.AnimationUtils;  
  12. import android.view.animation.Animation.AnimationListener;  
  13. import android.widget.AdapterView.OnItemClickListener;  
  14. import android.widget.ArrayAdapter;  
  15. import android.widget.ListView;  
  16. import android.widget.RelativeLayout;  
  17. import android.widget.TextView;  
  18.   
  19. public class ItemDialog extends android.app.Dialog {  
  20.   
  21.     Context context;  
  22.     View view;  
  23.     View backView;  
  24.     ListView listView;  
  25.     ArrayAdapter<String> adapter;  
  26.   
  27.     String[] messages;  
  28.     String title;  
  29.     TextView titleTextView;  
  30.       
  31.     OnItemClickListener itemClickListener;  
  32.   
  33.     public ItemDialog(Context context, String title, String[] messages) {  
  34.         super(context, android.R.style.Theme_Translucent);  
  35.         this.context = context;  
  36.         this.messages = messages;  
  37.         this.title = title;  
  38.     }  
  39.   
  40.     @Override  
  41.     protected void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  44.         super.onCreate(savedInstanceState);  
  45.         setContentView(R.layout.dialog);  
  46.   
  47.         view = (RelativeLayout) findViewById(R.id.contentDialog);  
  48.         backView = (RelativeLayout) findViewById(R.id.dialog_rootView);  
  49.         backView.setOnTouchListener(new OnTouchListener() {  
  50.   
  51.             @SuppressLint("ClickableViewAccessibility")  
  52.             public boolean onTouch(View v, MotionEvent event) {  
  53.                 if (event.getX() < view.getLeft()  
  54.                         || event.getX() > view.getRight()  
  55.                         || event.getY() > view.getBottom()  
  56.                         || event.getY() < view.getTop()) {  
  57.                     dismiss();  
  58.                 }  
  59.                 return false;  
  60.             }  
  61.         });  
  62.   
  63.         this.titleTextView = (TextView) findViewById(R.id.title);  
  64.         setTitle(title);  
  65.           
  66.         this.listView = (ListView) findViewById(R.id.list_view);  
  67.         adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, messages);  
  68.         listView.setAdapter(adapter);  
  69.           
  70.     }  
  71.     //get title  
  72.     public String getTitle() {  
  73.         return title;  
  74.     }  
  75.     //set title  
  76.     public void setTitle(String title) {  
  77.         this.title = title;  
  78.         if(title == null)  
  79.             titleTextView.setVisibility(View.GONE);  
  80.         else{  
  81.             titleTextView.setVisibility(View.VISIBLE);  
  82.             titleTextView.setText(title);  
  83.         }  
  84.     }  
  85.       
  86.     @Override  
  87.     public void dismiss() {  
  88.         Animation anim = AnimationUtils.loadAnimation(context,  
  89.                 R.anim.dialog_main_hide_amination);  
  90.         anim.setAnimationListener(new AnimationListener() {  
  91.   
  92.             public void onAnimationStart(Animation animation) {  
  93.             }  
  94.   
  95.             public void onAnimationRepeat(Animation animation) {  
  96.             }  
  97.   
  98.             public void onAnimationEnd(Animation animation) {  
  99.                 view.post(new Runnable() {  
  100.                     public void run() {  
  101.                         ItemDialog.super.dismiss();  
  102.                     }  
  103.                 });  
  104.   
  105.             }  
  106.         });  
  107.         Animation backAnim = AnimationUtils.loadAnimation(context,  
  108.                 R.anim.dialog_root_hide_amin);  
  109.   
  110.         view.startAnimation(anim);  
  111.         backView.startAnimation(backAnim);  
  112.     }  
  113.       
  114.     public void setOnItemClickListener(OnItemClickListener itemClickListener){  
  115.         this.itemClickListener = itemClickListener;  
  116.         if(listView != null){  
  117.             listView.setOnItemClickListener(itemClickListener);  
  118.         }  
  119.     }  
  120. }  
  121. </span>  

代码中主要是处理TextView的数据加载初始化,以及listview的数据添加,事件处理的接口。再有就是用户点击对话框外的区域就行对话框的关闭。


最后是使用方法:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">package com.testdialog.testmydialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.AdapterView;  
  8. import android.widget.Toast;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10.   
  11. public class MainActivity extends Activity {  
  12.       
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
  20.               
  21.             public void onClick(View v) {  
  22.                 showItemDialog();  
  23.             }  
  24.         });  
  25.     }  
  26.       
  27.     private void showItemDialog(){  
  28.         String[] messages = {"百度地图","卫星地图","热力地图"};  
  29.         ItemDialog dialog = new ItemDialog(this"请选择地图类型", messages);  
  30.         dialog.show();  
  31.         dialog.setOnItemClickListener(new OnItemClickListener() {  
  32.   
  33.             public void onItemClick(AdapterView<?> parent, View view,  
  34.                     int position, long id) {  
  35.                 Toast.makeText(MainActivity.this"王灿"+position, Toast.LENGTH_SHORT).show();  
  36.             }  
  37.         });  
  38.     }  
  39. }</span>  

最后,附上完整源码:

http://download.csdn.net/detail/cassiepython/9117091

Android:自定义Material Design风格的Dialog


项目需要,所以来做一个Material Design风格的选择的对话框。

界面如下所示:



先来看看布局文件:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/dialog_rootView"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#55000000"  
  7.     android:padding="32dp" >  
  8.   
  9.     <RelativeLayout  
  10.         android:id="@+id/contentDialog"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_centerInParent="true"  
  14.         android:background="@drawable/dialog_background"  
  15.         android:padding="24dp" >  
  16.   
  17.         <TextView  
  18.             android:id="@+id/title"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_marginBottom="20dp"  
  22.             android:text="Title"  
  23.             android:textAppearance="?android:attr/textAppearanceLarge"  
  24.             android:textColor="#000" />  
  25.   
  26.         <LinearLayout  
  27.             android:layout_width="match_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:layout_below="@id/title"  
  30.             android:orientation="vertical"  
  31.             android:paddingBottom="20dp" >  
  32.   
  33.             <ListView  
  34.                 android:id="@+id/list_view"  
  35.                 android:layout_width="match_parent"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:footerDividersEnabled="true" >  
  38.             </ListView>  
  39.         </LinearLayout>  
  40.     </RelativeLayout>  
  41. </RelativeLayout></span>  

实现起来比较简单,我们看图就知道 ,我们需要一个title,也就是标题栏,所以布局中有一个TextView来承载标题的文本内容,下面就是一个listview,用于加载我们的每一项。
所以我们在代码中主要就是要处理后这两个控件。

自定义Dialog代码:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">package com.testdialog.testmydialog;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.Window;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.AnimationUtils;  
  12. import android.view.animation.Animation.AnimationListener;  
  13. import android.widget.AdapterView.OnItemClickListener;  
  14. import android.widget.ArrayAdapter;  
  15. import android.widget.ListView;  
  16. import android.widget.RelativeLayout;  
  17. import android.widget.TextView;  
  18.   
  19. public class ItemDialog extends android.app.Dialog {  
  20.   
  21.     Context context;  
  22.     View view;  
  23.     View backView;  
  24.     ListView listView;  
  25.     ArrayAdapter<String> adapter;  
  26.   
  27.     String[] messages;  
  28.     String title;  
  29.     TextView titleTextView;  
  30.       
  31.     OnItemClickListener itemClickListener;  
  32.   
  33.     public ItemDialog(Context context, String title, String[] messages) {  
  34.         super(context, android.R.style.Theme_Translucent);  
  35.         this.context = context;  
  36.         this.messages = messages;  
  37.         this.title = title;  
  38.     }  
  39.   
  40.     @Override  
  41.     protected void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  44.         super.onCreate(savedInstanceState);  
  45.         setContentView(R.layout.dialog);  
  46.   
  47.         view = (RelativeLayout) findViewById(R.id.contentDialog);  
  48.         backView = (RelativeLayout) findViewById(R.id.dialog_rootView);  
  49.         backView.setOnTouchListener(new OnTouchListener() {  
  50.   
  51.             @SuppressLint("ClickableViewAccessibility")  
  52.             public boolean onTouch(View v, MotionEvent event) {  
  53.                 if (event.getX() < view.getLeft()  
  54.                         || event.getX() > view.getRight()  
  55.                         || event.getY() > view.getBottom()  
  56.                         || event.getY() < view.getTop()) {  
  57.                     dismiss();  
  58.                 }  
  59.                 return false;  
  60.             }  
  61.         });  
  62.   
  63.         this.titleTextView = (TextView) findViewById(R.id.title);  
  64.         setTitle(title);  
  65.           
  66.         this.listView = (ListView) findViewById(R.id.list_view);  
  67.         adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, messages);  
  68.         listView.setAdapter(adapter);  
  69.           
  70.     }  
  71.     //get title  
  72.     public String getTitle() {  
  73.         return title;  
  74.     }  
  75.     //set title  
  76.     public void setTitle(String title) {  
  77.         this.title = title;  
  78.         if(title == null)  
  79.             titleTextView.setVisibility(View.GONE);  
  80.         else{  
  81.             titleTextView.setVisibility(View.VISIBLE);  
  82.             titleTextView.setText(title);  
  83.         }  
  84.     }  
  85.       
  86.     @Override  
  87.     public void dismiss() {  
  88.         Animation anim = AnimationUtils.loadAnimation(context,  
  89.                 R.anim.dialog_main_hide_amination);  
  90.         anim.setAnimationListener(new AnimationListener() {  
  91.   
  92.             public void onAnimationStart(Animation animation) {  
  93.             }  
  94.   
  95.             public void onAnimationRepeat(Animation animation) {  
  96.             }  
  97.   
  98.             public void onAnimationEnd(Animation animation) {  
  99.                 view.post(new Runnable() {  
  100.                     public void run() {  
  101.                         ItemDialog.super.dismiss();  
  102.                     }  
  103.                 });  
  104.   
  105.             }  
  106.         });  
  107.         Animation backAnim = AnimationUtils.loadAnimation(context,  
  108.                 R.anim.dialog_root_hide_amin);  
  109.   
  110.         view.startAnimation(anim);  
  111.         backView.startAnimation(backAnim);  
  112.     }  
  113.       
  114.     public void setOnItemClickListener(OnItemClickListener itemClickListener){  
  115.         this.itemClickListener = itemClickListener;  
  116.         if(listView != null){  
  117.             listView.setOnItemClickListener(itemClickListener);  
  118.         }  
  119.     }  
  120. }  
  121. </span>  

代码中主要是处理TextView的数据加载初始化,以及listview的数据添加,事件处理的接口。再有就是用户点击对话框外的区域就行对话框的关闭。


最后是使用方法:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">package com.testdialog.testmydialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.AdapterView;  
  8. import android.widget.Toast;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10.   
  11. public class MainActivity extends Activity {  
  12.       
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
  20.               
  21.             public void onClick(View v) {  
  22.                 showItemDialog();  
  23.             }  
  24.         });  
  25.     }  
  26.       
  27.     private void showItemDialog(){  
  28.         String[] messages = {"百度地图","卫星地图","热力地图"};  
  29.         ItemDialog dialog = new ItemDialog(this"请选择地图类型", messages);  
  30.         dialog.show();  
  31.         dialog.setOnItemClickListener(new OnItemClickListener() {  
  32.   
  33.             public void onItemClick(AdapterView<?> parent, View view,  
  34.                     int position, long id) {  
  35.                 Toast.makeText(MainActivity.this"王灿"+position, Toast.LENGTH_SHORT).show();  
  36.             }  
  37.         });  
  38.     }  
  39. }</span>  

最后,附上完整源码:

http://download.csdn.net/detail/cassiepython/9117091
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值