Android項目實戰(七):Dialog主題Activity實現自定義對話框效果

想必你們都用過Dialog主題的Activity吧,用它來顯示自定義對話框效果絕對是一個很是不錯的選擇。android

即把activity交互界面以Dialog的形式展示出來,Dialog主題的Activity大小將之內容的寬高來決定ide

<activity android:name=MainActivity
android:theme
=」@android:style/Theme.Dialog」>
</activity>

能夠看到設置爲Theme.Dialog主題的activity顯示效果,佈局

是相似對話框的形式顯示出來的,而背景則是這個Activity的上一個activity交互界面,spa

或者若是此Activity是程序第一個Activity,背景則是手機桌面code

那麼讓咱們本身作一個漂亮點的對話框形式的Activityxml

首先,要把Activity自帶的標題去掉blog

使用 requestWindowFeature(Window.FEATURE_NO_TITLE); 語句圖片

注意 須要在 setContentView(R.layout.main); 語句以前使用ip

畫一個佈局,具體效果看本身的Xml寫的怎麼樣了utf-8

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:orientation="vertical"
        android:background="@drawable/duanxinbeijing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="0.0dip"
            android:layout_weight="2.0">
        <TextView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15.0dip"
                android:layout_marginBottom="15.0dip"
                android:layout_weight="1.0" />
    </LinearLayout>
    <TextView
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15.0dip"
            android:text="是否撥打電話"
            android:textSize="20sp"
            android:layout_weight="1.0" />
    <View
            android:background="#ffd1d1d1"
            android:layout_width="fill_parent"
            android:layout_height="2.0px" />
    <LinearLayout
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <ImageButton
                android:id="@+id/call_dialog_queren"
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_marginTop="10.0dip"
                android:layout_marginBottom="10.0dip"
                android:src="@drawable/dialogqueren"
                android:background="#0000"
                android:layout_weight="1.0" />
        <ImageButton
                android:id="@+id/call_dialog_quxiao"
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_marginTop="10.0dip"
                android:layout_marginBottom="10.0dip"
                android:src="@drawable/dialogquxiao"
                android:background="#0000"
                android:layout_weight="1.0" />
    </LinearLayout>
</LinearLayout>
View Code

Activity關鍵代碼:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //去除這個Activity的標題欄
        setContentView(R.layout.main);
    }

看效果圖:

------------------------------------------------------------------------------------------------

當前,上述是咱們你們通常使用的操做,可是,若是咱們須要一個更加漂亮,用戶體驗更好的,好比說圓角對話框呢,而上述方法能夠明顯的看到當背景是圓角圖片的時候,四個角的效果是十分差的。 android:theme=」@android:style/Theme.Dialog」 主題的Activity是方方正正的對話框樣式的。

實現方法就是 自定義一個style ,在res/styles.xml 文件中

<style name="MyDialogStyle">
        <item name="android:windowBackground">@android:color/transparent</item> 設置dialog的背景,此處爲系統給定的透明值
        <item name="android:windowFrame">@null</item>                Dialog的windowFrame框爲無
        <item name="android:windowNoTitle">true</item>         是否顯示標題
        <item name="android:windowIsFloating">true</item>            是否浮如今activity之上
        <item name="android:windowIsTranslucent">true</item>         是否半透明
        <item name="android:windowContentOverlay">@null</item>       是否有覆蓋
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>   設置Activity出現方式
        <item name="android:backgroundDimEnabled">true</item>        背景是否模糊顯示
</style>

佈局文件不變,再更改清單配置文件:

<activity
     android:name="MainActivity"
     android:theme="@style/MyDialogStyle" />  //主題設置爲咱們自定義的style
<activity

便可看到效果,是否是四個直角成爲背景圖片對應的圓角了

 

固然,它仍是一個Activity,因此能夠照常的對Activity裏面的控件進行操做

相關文章
相關標籤/搜索