分析
1、調整對話方塊後面的背景的透明度
2、將對話方塊頭部去掉
3、將對話方塊自身的圓角白色邊框替換為直角5px的白色邊框
4、設定對話方塊內部背景。
先來看一個效果
AlertDialog dialog = new AlertDialog.Builder(this).setTitle("title")
.setMessage("message").create();
Window window = alertDialog.getWindow();
window.setGravity(Gravity.TOP); //window.setGravity(Gravity.BOTTOM);
alertDialog.show();
透明的對話方塊
預設顯示的對話方塊是不透明的,但我們可以通過設定對話方塊的alpha值將其變成透明或半透明效果。我們都知道。顏色由R(紅)、G(綠)、B(藍)組成。除此之外,還會有一個A(透明度,Alpha)來描述顏色。在顏色的描述中,如果該值為0表示完全透明,如果該值為255,表示不透明。
通過設定Windows的alpha屬性也可以設定對話方塊的透明度。但alpha的取值範圍是從0到1.0。如果該屬性值為0,表示完全透明,如果該值為1.0,表示不透明(也就是正常顯示的對話方塊)。下面的代碼通過將alpha的值設為0.3,為了更清晰地顯示透明的對話方塊和非透明的對話方塊。在本例中加了一個背景映像,將同時顯示了兩個對話方塊(一個是半透明的,另一是不透明的)。
複製代碼 代碼如下:
// 顯示透明的對話方塊
AlertDialog alertDialog = new AlertDialog.Builder(this).setMessage(
"透明對話方塊").setPositiveButton("確定", null).create();
Window window = alertDialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
// 設定透明度為0.3
lp.alpha = 0.6f;
window.setAttributes(lp);
alertDialog.show();
我們在使用某些應用時會發現當彈出對話方塊或某些強制回應視窗時,後面的內容會變得模糊或不清楚。實際上,這些效果也很容易在OPhone中實現。為了實現這個功能,我們只需要設定Wndow對象的兩個標誌即可,代碼如下:
去掉白邊框
設定style,
<style name="myDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<!– <item name="android:background">@drawable/bg_sel</item>–>
<item name="android:windowBackground">@drawable/bg_sel</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
將此style方在構造方法中。
4、設定對話方塊內部背景,
自訂一個dialog模板視圖,裡面只有兩個linearlayout,所有的View都添加到裡面的linearlayout中即可。
如下代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="5px">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/linearLayout" android:background="#90333333"/>
</LinearLayout>
實現由下至上彈出並位於螢幕底部的提示框:
1. button.setOnClickListener(new OnClickListener() {
2.@Override 3.public void onClick(View arg0) { 4. // TODO Auto-generated method stub 5. AlertDialog dialog = new AlertDialog.Builder(TestAndroid1Activity.this) 6. .setTitle("title").setMessage("message").create(); 7. Window window = dialog.getWindow();
8. window.setGravity(Gravity.BOTTOM); //此處可以設定dialog顯示的位置 9. window.setWindowAnimations(R.style.mystyle); //添加動畫 10. dialog.show();
11.}
12.);
styles.xml
1.<?xml version="1.0" encoding="utf-8"?>
2.<resources>
3.
4. <style name="mystyle" parent="android:Animation"> 5. <item name="@android:windowEnterAnimation">@anim/dialog_enter</item> //進入時的動畫 6. <item name="@android:windowExitAnimation">@anim/dialog_exit</item> //退出時的動畫 7. </style>
8.</resources>
位於 res/anim/dialog_enter.xml
1.<?xml version="1.0" encoding="utf-8"?>
2.<set xmlns:android="http://schemas.android.com/apk/res/android"> 3.
4. <translate
5. android:fromYDelta="100%p" %p指相對於父容器 6. android:duration="600" 7. />
8.</set>
位於 res/anim/dialog_exit.xml
1.<?xml version="1.0" encoding="utf-8"?>
2.<set xmlns:android="http://schemas.android.com/apk/res/android"> 3.
4. <translate
5. android:toYDelta="100%p" 6. android:duration="600" //期間 7. />
8.</set>
此處只是做了垂直位移的效果,自己還可以試試別的效果。
<alpha /> 透明度
<rotate /> 旋轉
<scale /> 縮放