现在的位置: 首页 > 综合 > 正文

自定义widgt and inflater

2017年12月12日 ⁄ 综合 ⁄ 共 14436字 ⁄ 字号 评论关闭
Android高手进阶教程(八)之----Android Widget开发案例(世界杯倒计时!)

2010-04-29 21:11:25

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://weizhulin.blog.51cto.com/1556324/311438
今天我们要写一下Android Widget的开发,由于快点凌晨,我就不说的太具体了,同志们就模仿吧!首先看一下效果图:
 
下面是Demo的详细步骤:
一、新建一个Android工程命名为:WidgetDemo.
 
二、准备素材,一个是Widget的图标,一个是Widget的背景。存放目录如下图:
 
三、修改string.xml文件如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <resources>     
  4.     <string name="hello">Hello World, WidetDemo!</string>     
  5.     <string name="app_name">DaysToWorldCup</string>     
  6. </resources>    
  7. <?xml version="1.0" encoding="utf-8"?> 
  8. <resources> 
  9.     <string name="hello">Hello World, WidetDemo!</string> 
  10.     <string name="app_name">DaysToWorldCup</string> 
  11. </resources> 
 
四、建立Widget内容提供者文件,我们在res下建立xml文件夹,并且新建一个widget_provider.xml代码入下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     android:minWidth="50dip"    
  5.     android:minHeight="50dip"    
  6.     android:updatePeriodMillis="10000"    
  7.     android:initialLayout="@layout/main"    
  8. />     
  9. <?xml version="1.0" encoding="utf-8"?> 
  10. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
  11.  android:minWidth="50dip" 
  12.  android:minHeight="50dip" 
  13.  android:updatePeriodMillis="10000" 
  14.  android:initialLayout="@layout/main" 
  15. />    
五、修改main.xml布局,代码如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     android:orientation="vertical"    
  5.     android:layout_width="fill_parent"    
  6.     android:layout_height="fill_parent"    
  7.     android:background="@drawable/wordcup"    
  8.     >     
  9. <TextView       
  10.     android:id="@+id/wordcup"    
  11.     android:layout_width="fill_parent"      
  12.     android:layout_height="wrap_content"      
  13.     android:text="@string/hello"    
  14.     android:textSize="12px"    
  15.     android:textColor="#ff0000"    
  16.     />     
  17. </LinearLayout>    
  18. <?xml version="1.0" encoding="utf-8"?>  
  19. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  20.     android:orientation="vertical" 
  21.     android:layout_width="fill_parent" 
  22.     android:layout_height="fill_parent" 
  23.     android:background="@drawable/wordcup" 
  24.     >  
  25. <TextView    
  26.  android:id="@+id/wordcup" 
  27.     android:layout_width="fill_parent"   
  28.     android:layout_height="wrap_content"   
  29.     android:text="@string/hello" 
  30.   android:textSize="12px" 
  31.     android:textColor="#ff0000" 
  32.     />  
  33. </LinearLayout>  
  34.    
  35. 六、修改WidgetDemo.java代码如下:  
  36. view plaincopy to clipboardprint?  
  37. package com.android.tutor;     
  38. import java.util.Calendar;     
  39. import java.util.Date;     
  40. import java.util.GregorianCalendar;     
  41. import java.util.Timer;     
  42. import java.util.TimerTask;     
  43. import android.appwidget.AppWidgetManager;     
  44. import android.appwidget.AppWidgetProvider;     
  45. import android.content.ComponentName;     
  46. import android.content.Context;     
  47. import android.widget.RemoteViews;     
  48. public class WidetDemo extends AppWidgetProvider {     
  49.     /** Called when the activity is first created. */    
  50.        
  51.     @Override    
  52.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,     
  53.             int[] appWidgetIds) {     
  54.              
  55.         Timer timer = new Timer();     
  56.         timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 160000);     
  57.         super.onUpdate(context, appWidgetManager, appWidgetIds);     
  58.     }     
  59.          
  60.          
  61.     private class MyTime extends TimerTask{     
  62.         RemoteViews remoteViews;     
  63.         AppWidgetManager appWidgetManager;     
  64.         ComponentName thisWidget;     
  65.              
  66.         public MyTime(Context context,AppWidgetManager appWidgetManager){     
  67.             this.appWidgetManager = appWidgetManager;     
  68.             remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);     
  69.                  
  70.             thisWidget = new ComponentName(context,WidetDemo.class);     
  71.         }     
  72.         public void run() {     
  73.                  
  74.             Date date = new Date();     
  75.             Calendar calendar = new GregorianCalendar(2010,06,11);     
  76.             long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400;     
  77.             remoteViews.setTextViewText(R.id.wordcup, "距离南非世界杯还有" + days+"天");     
  78.             appWidgetManager.updateAppWidget(thisWidget, remoteViews);     
  79.                  
  80.         }     
  81.              
  82.     }     
  83.        
  84. }    
  85. package com.android.tutor;  
  86. import java.util.Calendar;  
  87. import java.util.Date;  
  88. import java.util.GregorianCalendar;  
  89. import java.util.Timer;  
  90. import java.util.TimerTask;  
  91. import android.appwidget.AppWidgetManager;  
  92. import android.appwidget.AppWidgetProvider;  
  93. import android.content.ComponentName;  
  94. import android.content.Context;  
  95. import android.widget.RemoteViews;  
  96. public class WidetDemo extends AppWidgetProvider {  
  97.     /** Called when the activity is first created. */ 
  98.     
  99.  @Override 
  100.  public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  101.    int[] appWidgetIds) {  
  102.     
  103.   Timer timer = new Timer();  
  104.   timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 160000);  
  105.   super.onUpdate(context, appWidgetManager, appWidgetIds);  
  106.  }  
  107.    
  108.    
  109.  private class MyTime extends TimerTask{  
  110.   RemoteViews remoteViews;  
  111.   AppWidgetManager appWidgetManager;  
  112.   ComponentName thisWidget;  
  113.     
  114.   public MyTime(Context context,AppWidgetManager appWidgetManager){  
  115.    this.appWidgetManager = appWidgetManager;  
  116.    remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);  
  117.      
  118.    thisWidget = new ComponentName(context,WidetDemo.class);  
  119.   }  
  120.   public void run() {  
  121.      
  122.    Date date = new Date();  
  123.    Calendar calendar = new GregorianCalendar(2010,06,11);  
  124.    long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400;  
  125.    remoteViews.setTextViewText(R.id.wordcup, "距离南非世界杯还有" + days+"天");  
  126.    appWidgetManager.updateAppWidget(thisWidget, remoteViews);  
  127.      
  128.   }  
  129.     
  130.  }  
  131.     
  132. }   
七、修改配置文件AndroidManifest.xml,代码如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
  4.       package="com.android.tutor"    
  5.       android:versionCode="1"    
  6.       android:versionName="1.0">     
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">     
  8.         <receiver android:name=".WidetDemo"    
  9.                   android:label="@string/app_name">     
  10.             <intent-filter>     
  11.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />     
  12.             </intent-filter>     
  13.             <meta-data android:name="android.appwidget.provider"    
  14.                        android:resource="@xml/widget_provider"    
  15.             />     
  16.         </receiver>     
  17.     </application>     
  18.     <uses-sdk android:minSdkVersion="7" />     
  19. </manifest>     
  20. <?xml version="1.0" encoding="utf-8"?> 
  21. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  22.       package="com.android.tutor" 
  23.       android:versionCode="1" 
  24.       android:versionName="1.0"> 
  25.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  26.         <receiver android:name=".WidetDemo" 
  27.                   android:label="@string/app_name"> 
  28.             <intent-filter> 
  29.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
  30.             </intent-filter> 
  31.             <meta-data android:name="android.appwidget.provider" 
  32.                  android:resource="@xml/widget_provider" 
  33.             /> 
  34.         </receiver> 
  35.     </application> 
  36.     <uses-sdk android:minSdkVersion="7" /> 
  37. </manifest>    
八、点击运行(Ctrl+F11),之,运行成功后,我们长时间点击桌面,会出现如下俩个,依次点击,就可以看到最上面的效果图:
 
今天就到这里了,我困了呵呵,我发现时间好像不对劲,lol~我也不去多想了,大家知道的告诉我下!对日历这些东西不是太了解,谢谢!!
 
Android高手进阶教程(五)之----Android 中LayoutInflater的使用!

2010-04-21 21:11:25

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://weizhulin.blog.51cto.com/1556324/311450
大家好我们这一节讲的是LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
效果图如下:
 
下面我将详细的说明Demo的实现过程:
1、新建一个 Android工程,我们命名为LayoutInflaterDemo.
2、修改main.xml布局,里面主要在原来基础上增加了一个Button.代码如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0"      
  3. encoding="utf-8"?>     
  4. <LinearLayout      
  5. xmlns:android="http://schemas.android.com/apk/res/android"    
  6.     android:orientation="vertical"    
  7.     android:layout_width="fill_parent"    
  8.     android:layout_height="fill_parent"    
  9.     >     
  10. <TextView       
  11.     android:layout_width="fill_parent"      
  12.     android:layout_height="wrap_content"      
  13.     android:text="@string/hello"    
  14.     />     
  15. <Button     
  16.     android:id="@+id/button"    
  17.     android:layout_width="wrap_content"    
  18.     android:layout_height="wrap_content"    
  19.     android:text="ShowCustomDialog"    
  20.     />     
  21. </LinearLayout>    
  22. <?xml version="1.0"   
  23. encoding="utf-8"?> 
  24. <LinearLayout   
  25. xmlns:android="http://schemas.android.com/apk/res/android" 
  26.     android:orientation="vertical" 
  27.     android:layout_width="fill_parent" 
  28.     android:layout_height="fill_parent" 
  29.     > 
  30. <TextView    
  31.     android:layout_width="fill_parent"   
  32.     android:layout_height="wrap_content"   
  33.     android:text="@string/hello" 
  34.     /> 
  35. <Button 
  36.  android:id="@+id/button" 
  37.  android:layout_width="wrap_content" 
  38.  android:layout_height="wrap_content" 
  39.  android:text="ShowCustomDialog" 
  40.  /> 
  41. </LinearLayout> 
 
3.定义对话框的布局方式,我们在layout目录下,新建一个名为 custom_dialog.xml文件具体代码如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0"      
  3. encoding="utf-8"?>     
  4. <LinearLayout      
  5. xmlns:android="http://schemas.android.com/apk/res/android"    
  6.               android:orientation="horizontal"    
  7.               android:layout_width="fill_parent"    
  8.               android:layout_height="fill_parent"    
  9.               android:padding="10dp"    
  10.               >     
  11.     <ImageView android:id="@+id/image"    
  12.                android:layout_width="wrap_content"    
  13.                android:layout_height="fill_parent"    
  14.                android:layout_marginRight="10dp"    
  15.                />     
  16.     <TextView android:id="@+id/text"    
  17.               android:layout_width="wrap_content"    
  18.               android:layout_height="fill_parent"    
  19.               android:textColor="#FFF"    
  20.               />     
  21. </LinearLayout>    
  22. <?xml version="1.0"   
  23. encoding="utf-8"?> 
  24. <LinearLayout   
  25. xmlns:android="http://schemas.android.com/apk/res/android" 
  26.               android:orientation="horizontal" 
  27.               android:layout_width="fill_parent" 
  28.               android:layout_height="fill_parent" 
  29.               android:padding="10dp" 
  30.               > 
  31.     <ImageView android:id="@+id/image" 
  32.                android:layout_width="wrap_content" 
  33.                android:layout_height="fill_parent" 
  34.                android:layout_marginRight="10dp" 
  35.                /> 
  36.     <TextView android:id="@+id/text" 
  37.               android:layout_width="wrap_content" 
  38.               android:layout_height="fill_parent" 
  39.               android:textColor="#FFF" 
  40.               /> 
  41. </LinearLayout> 
 
4.修改主程序LayouInflaterDemo.java代码如下:
 
  1. view plaincopy to clipboardprint?  
  2. package com.android.tutor;     
  3. import android.app.Activity;     
  4. import android.app.AlertDialog;     
  5. import android.content.Context;     
  6. import android.os.Bundle;     
  7. import android.view.LayoutInflater;     
  8. import android.view.View;     
  9. import android.view.View.OnClickListener;     
  10. import android.widget.Button;     
  11. import android.widget.ImageView;     
  12. import android.widget.TextView;     
  13. public class LayoutInflaterDemo extends Activity implements      
  14. OnClickListener {     
  15.          
  16.     private Button button;     
  17.     public void onCreate(Bundle savedInstanceState) {     
  18.         super.onCreate(savedInstanceState);     
  19.         setContentView(R.layout.main);     
  20.              
  21.         button = (Button)findViewById(R.id.button);     
  22.         button.setOnClickListener(this);     
  23.     }     
  24.     @Override    
  25.     public void onClick(View v) {     
  26.              
  27.         showCustomDialog();     
  28.     }     
  29.          
  30.     public void showCustomDialog()     
  31.     {     
  32.         AlertDialog.Builder builder;     
  33.         AlertDialog alertDialog;     
  34.         Context mContext = LayoutInflaterDemo.this;     
  35.              
  36.         //下面俩种方法都可以     
  37.         ////LayoutInflater inflater = getLayoutInflater();     
  38.         LayoutInflater inflater = (LayoutInflater)      
  39. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);     
  40.         View layout = inflater.inflate(R.layout.custom_dialog,null);     
  41.         TextView text = (TextView) layout.findViewById(R.id.text);     
  42.         text.setText("Hello, Welcome to Mr Wei's blog!");     
  43.         ImageView image = (ImageView) layout.findViewById(R.id.image);     
  44.         image.setImageResource(R.drawable.icon);     
  45.         builder = new AlertDialog.Builder(mContext);     
  46.         builder.setView(layout);     
  47.         alertDialog = builder.create();     
  48.         alertDialog.show();     
  49.     }     
  50. }    
  51. package com.android.tutor;  
  52. import android.app.Activity;  
  53. import android.app.AlertDialog;  
  54. import android.content.Context;  
  55. import android.os.Bundle;  
  56. import android.view.LayoutInflater;  
  57. import android.view.View;  
  58. import android.view.View.OnClickListener;  
  59. import android.widget.Button;  
  60. import android.widget.ImageView;  
  61. import android.widget.TextView;  
  62. public class LayoutInflaterDemo extends Activity implements   
  63. OnClickListener {  
  64.       
  65.  private Button button;  
  66.     public void onCreate(Bundle savedInstanceState) {  
  67.         super.onCreate(savedInstanceState);  
  68.         setContentView(R.layout.main);  
  69.           
  70.         button = (Button)findViewById(R.id.button);  
  71.         button.setOnClickListener(this);  
  72.     }  
  73.  @Override 
  74.  public void onClick(View v) {  
  75.     
  76.   showCustomDialog();  
  77.  }  
  78.    
  79.  public void showCustomDialog()  
  80.  {  
  81.   AlertDialog.Builder builder;  
  82.   AlertDialog alertDialog;  
  83.   Context mContext = LayoutInflaterDemo.this;  
  84.     
  85.   //下面俩种方法都可以  
  86.   ////LayoutInflater inflater = getLayoutInflater();  
  87.   LayoutInflater inflater = (LayoutInflater)   
  88. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
  89.   View layout = inflater.inflate(R.layout.custom_dialog,null);  
  90.   TextView text = (TextView) layout.findViewById(R.id.text);  
  91.   text.setText("Hello, Welcome to Mr Wei's blog!");  
  92.   ImageView image = (ImageView) layout.findViewById(R.id.image);  
  93.   image.setImageResource(R.drawable.icon);  
  94.   builder = new AlertDialog.Builder(mContext);  
  95.   builder.setView(layout);  
  96.   alertDialog = builder.create();  
  97.   alertDialog.show();  
  98.  }  
  99. }   
5、最后执行之,点击Button,将得到上述效果。
 好今天就到此为止,睡觉了,大家有什么不明白的请留言~谢谢!
 

抱歉!评论已关闭.