1. 程式人生 > >對話方塊樣式的activity實現與美化

對話方塊樣式的activity實現與美化

轉自:http://www.apkbus.com/thread-166567-1-1.html?fromuid=321014

很多時候,我們需要在桌面或者其他地方(當前view不是Activity的情況)彈出一個提示對話方塊,例如鬧鐘提示,系統錯誤提示等,如果直接用dialog實現這樣的對話方塊就會報錯,原因是dialog對話方塊在例項化時必須有一個Context型別引數,即對話方塊的彈出式和當前activity相聯絡起來的,這種辦法行不通。既然直接用dialog不行,那麼我們就用Activity實現吧,不過這個Activity看起來和dialog一樣。具體實現如下:

先定義一個佈局檔案dialog.xm:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >

<TextView
android:id="@+id/dialog_tv"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="17dp"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
android:id="@+id/b1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/select_text"
/>

<Button
android:id="@+id/b2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/select_text"
/>
</LinearLayout>

</LinearLayout>
然後在清單檔案裡面定義Activity的樣式:
<activity
android:name="com.example.sidebar2.dialog"
android:theme="@android:style/Theme.Dialog" >
</activity>
接下來在dialog類中載入檢視就可以了,具體效果如下:


但是感覺這樣的視窗太醜了,一點都不美觀,沒辦法,系統提供的東西就這樣。想要好看點就自己動手吧。我們先在drawable資料夾下新建一個filled_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#dd0a0000" />

<stroke
android:width="3dp"
android:color="#980a0000" />

<corners android:radius="20dp" />

<padding
android:bottom="10dp"
android:left="5dp"
android:right="5dp"
android:top="10dp" />

</shape>

這裡,
solid 也就是實體話  填充色的意思 就是整個背景的顏色
stroke 就是描邊的意思 也就是邊緣的顏色如果設定不仔細是沒有效果的 有點要求這個
corners就是圓角的實現。
然後,在values資料夾下的styles.xml檔案中加入如下程式碼
<style name="Theme" parent="android:Theme"></style>

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/filled_box</item>
<item name="android:windowNoTitle">true</item>
</style>
下一步就是新建一個佈局檔案dialog2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:src="@drawable/gantan" />

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:text="溫馨提示"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#838186" />

<TextView
android:id="@+id/dialogtv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:textColor="#838186"
android:textSize="25sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >

<Button
android:id="@+id/dialog_button1"
android:layout_marginLeft="8dp"
android:layout_width="140dp"
android:text="確認"
android:textSize="17sp"
android:layout_height="match_parent"
android:background="@drawable/select_text" />

<Button
android:id="@+id/dialog_button2"
android:layout_marginLeft="5dp"
android:textSize="17sp"
android:layout_width="140dp"
android:text="取消"
android:layout_height="match_parent"
android:background="@drawable/select_text2" />
</LinearLayout>

</LinearLayout>

其中的select_text.xml檔案是使按鈕有按下的感覺,就是按下顏色變化
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/queren1" android:state_pressed="false"/>
<item android:drawable="@drawable/queren2" android:state_pressed="true"/>
<item android:drawable="@drawable/queren2" android:state_focused="true"/>
<item android:drawable="@drawable/queren1"/>

</selector>

最後就在清單檔案中宣告自己定義的樣式就可以了
<activity
android:name="com.example.sidebar2.dialog"
android:theme="@style/Theme.CustomDialog" >
</activity>

具體結果如下怎麼樣,比系統自帶的漂亮多了吧。