[轉]Android有趣的全透明效果--Activity及Dialog的全透明(附android系統內建表徵圖大全

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   os   io   

原文:http://blog.csdn.net/sodino/article/details/58221471.Activity全透明

同學zzm給了這個有趣的代碼,現在公布出來。

先在res/values下建colors.xml檔案,寫入:

 

  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < resources >   
  3.     < color   name = "transparent" > #9000 </ color >   
  4. </ resources >   

這個值設定了整個介面的透明度,為了看得見效果,現在設為透明度為56%(9/16)左右。

再在res/values/下建styles.xml,設定程式的風格

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <style name="Transparent">
  4.         <item name="android:windowBackground">@color/transparent</item>
  5.         <item name="android:windowIsTranslucent">true</item>   
  6.         <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
  7.     </style>
  8. </resources>

最後一步,把這個styles.xml用在相應的Activity上。即在AndroidManifest.xml中的任 意<activity>標籤中添加

 

  1. android:theme = "@style/transparent"

如果想設定所有的activity都使用這個風格,可以把這句標籤語句添加在<application>中。

最後運行程式,哈哈,是不是發現整個介面都被蒙上一層半透明了。最後可以把背景色#9000換成#0000,運行程式後,就全透明了,看得見背景下 的所有東西可以卻都操作無效。呵呵....

 

2.Dialog全透明

1.準備保留邊框的全透明素材如:

2.在values中建立一styles.xml檔案,內容如下:

 

  • <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <style name="TANCStyle" parent="@android:style/Theme.Dialog">
            <!-- 更換背景圖片實現全透明 -->
            <item name="android:windowBackground">@drawable/panel_background_sodino1</item>
            <!-- 螢幕背景不變暗 -->
            <item name="android:backgroundDimEnabled">false</item>
            <!-- 更改對話方塊標題列 -->
            <item name="android:windowTitleStyle">@style/TitleStyle</item>
        </style>
        <style name="TitleStyle" parent="@android:style/DialogWindowTitle">
            <item name="android:textAppearance">@style/TitleText</item>
        </style>
        <style name="TitleText" parent="@android:style/TextAppearance.DialogWindowTitle">
            <!-- 設定Dialog標題列文字顏色。 -->
            <item name="android:textColor">#000</item>
        </style>
    </resources>

 

 

3.在layout檔案夾下建立一檔案句為main_dialog.xml,內容如下:

 

[xhtml] view plaincopy 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#0000">  
  6.     <ScrollView android:id="@+id/ScrollView01"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="200px"  
  9.         android:layout_below="@+id/ImageView01"  
  10.         android:background="#0000">  
  11.         <TextView android:id="@+id/TextView01"  
  12.             android:text="SodinoText"  
  13.             android:textColor="#f000"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:background="#0000"  
  17.         ></TextView>  
  18.     </ScrollView>  
  19.     <Button android:id="@+id/btnCancel"  
  20.         android:layout_below="@id/ScrollView01"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_centerHorizontal="true"  
  24.         android:text="Cancel">  
  25.     </Button>  
  26. </RelativeLayout>  

 

 

4.Activity代碼如下:

 

[xhtml] view plaincopy 
  1. package lab.sodino.tanc;  
  2. import android.app.Activity;  
  3. import android.app.Dialog;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8. public class TANCAct extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         Button btnShow = (Button) findViewById(R.id.btnShow);  
  15.         btnShow.setOnClickListener(new Button.OnClickListener() {  
  16.             public void onClick(View view) {  
  17.                 showTANC(  
  18.                         "This is my custom dialog box",  
  19.                         "TextContent/nWhen a dialog is requested for the first time, Android calls onCreateDialog(int)  from your Activity, which is where you should instantiate the Dialog. This callback method is passed the same ID that you passed to showDialog(int). After you create the Dialog, return the object at the end of the method.",  
  20.                         "http://blog.csdn.net/sodino");  
  21.             }  
  22.         });  
  23.     }  
  24.     private void showTANC(String header, String content, String url) {  
  25.         final Dialog dialog = new Dialog(this, R.style.TANCStyle);  
  26.         dialog.setContentView(R.layout.main_dialog);  
  27.         dialog.setTitle(header);  
  28.         dialog.setCancelable(true);  
  29.         TextView textView01 = (TextView) dialog.findViewById(R.id.TextView01);  
  30.         textView01.setText(content + content + content);  
  31.         Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);  
  32.         btnCancel.setOnClickListener(new Button.OnClickListener() {  
  33.             public void onClick(View view) {  
  34.                 dialog.cancel();  
  35.             }  
  36.         });  
  37.         dialog.show();  
  38.     }  
  39. }  

 

 

最後:

 

 

 

另附 android系統內建表徵圖大全(1.5 1.6 2.1)

http://since2006.com/android/1.5-drawables.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.