Android下Notification,樣式style,主題theme的功能實現


一:Notification

1.NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2. notification.setLatestEventInfo(this"國安部給你發短信!""你被通輯啦~~~"contentIntent);

  2.1: PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

2.1.1: Intent intent = new Intent();

  intent.setAction(Intent.ACTION_CALL);

 intent.setData(Uri.parse("tel:110"));

3.nm.notify(0,notification);

3.1:Notification notification = new

 Notification(R.drawable.ic_launcher,"有新的消息到來了",System.currentTimeMillis());

4.為避免成為流氓軟件,要: notification.flags = Notification.FLAG_AUTO_CANCEL;

詳細代碼:

 

  1. public class MainActivity extends Activity {  
  2.   
  3.     private NotificationManager nm;  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  8.     }  
  9.     // 通知是顯示在一個系統應用里面的, 如果系統應用ui掛了,通知就不會出來了。  
  10.     public void click(View view) {  
  11.          @SuppressWarnings("deprecation")  
  12.          Notification notification = new  
  13.          Notification(R.drawable.ic_launcher,"有新的消息到來了",System.currentTimeMillis());           
  14.          notification.flags = Notification.FLAG_AUTO_CANCEL;           
  15. //       //延期的意圖,不是立刻執行的意圖,最終這個意圖是在另外一個應用程序里面執行  
  16.          Intent intent = new Intent();  
  17.          intent.setAction(Intent.ACTION_CALL);  
  18.          intent.setData(Uri.parse("tel:110"));         
  19.          PendingIntent contentIntent = PendingIntent.getActivity(this0, intent, 0);          
  20.          //舊的api,一定要記得設置notification這一個參數  
  21.          notification.setLatestEventInfo(this"國安部給你發短信!""你被通輯啦~~~", contentIntent);   
  22.          nm.notify(0,notification);  
  23.       }  
  24.   }  


 

運行結果:

 

二:樣式:style(只能作用在控件上)

1.res/values/styles.xml設置樣式

 <style name="text_content_style">

        <!-- item里面是鍵值對! -->

        <item name="android:layout_width">wrap_content</item>

        <item name="android:layout_height">wrap_content</item>

        <item name="android:textColor">#FF0000</item>

        <item name="android:textSize">20sp</item>

    </style>

    

   繼承形式一 

   <style name="text_title_style" parent="@style/text_content_style">

       <item name="android:textSize">30sp</item>

   </style>

   

   繼承形式二 

   <style name="text_subtitle_style" parent="@style/text_content_style">

       <item name="android:textSize">25sp</item>

   </style>

2.使用樣式:

    <TextView 

        style="@style/text_title_style"

        android:text="我是標題"/>

    <TextView 

        style="@style/text_subtitle_style"

        android:text="我是sub標題"/>

    <TextView 

        style="@style/text_content_style"

        android:text="我是文本"/>

3.效果圖:

 

三:主題theme(作用在Activity或整個應用程序上Application)

1.style.xml中配置:

參考:~\adt-bundle-windows-x86-20130729\sdk\platforms\android-16\data\res\values\themes.xml

162行:Window attributes,可配置自己的Window attribute的主題。

注意:不同於系統的,自己要定義需要在name的字符串前加上:android:   如:

系統:

<item name="windowBackground">@android:drawable/screen_background_selector_dark</item>

自己:

 <item name="android:windowBackground">@color/red</item>

 

    <style name="red_theme">

        <item name="android:windowBackground">@color/red</item>

</style>

 

2.在AndroidMainfest.xml中使用:

 <activity

            android:theme="@style/red_theme"

...

 

效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM