設置theme實現類似於自定義dialog效果

我們知道我們要實現自定義dialog可以自己自定義,那麼還有一種方法就是讓activity變成窗體,那麼我們在設置Theme就可以了
我們來看效果
這裏寫圖片描述
點擊後彈出一個類似氣泡的東西

Android平臺定義的主題樣式:

android:theme=”@android:style/Theme.Dialog” 將一個Activity顯示爲對話框模式

•android:theme=”@android:style/Theme.NoTitleBar” 不顯示應用程序標題欄
•android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” 不顯示應用程序標題欄,並全屏

•android:theme=”@android:style/Theme.Light” 背景爲白色
•android:theme=”@android:style/Theme.Light.NoTitleBar” 白色背景並無標題欄
•android:theme=”@android:style/Theme.Light.NoTitleBar.Fullscreen” 白色背景,無標題欄,全屏

•android:theme=”@android:style/Theme.Black” 背景黑色
•android:theme=”@android:style/Theme.Black.NoTitleBar” 黑色背景並無標題欄
•android:theme=”@android:style/Theme.Black.NoTitleBar.Fullscreen” 黑色背景,無標題欄,全屏

•android:theme=”@android:style/Theme.Wallpaper” 用系統桌面爲應用程序背景
•android:theme=”@android:style/Theme.Wallpaper.NoTitleBar” 用系統桌面爲應用程序背景,且無標題欄
•android:theme=”@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen” 用系統桌面爲應用程序背景,無標題欄,全屏

•android:theme=”@android:style/Translucent” 半透明效果
•android:theme=”@android:style/Theme.Translucent.NoTitleBar” 半透明並無標題欄
•android:theme=”@android:style/Theme.Translucent.NoTitleBar.Fullscreen” 半透明效果,無標題欄,全屏
•android:theme=”@android:style/Theme.Panel”
我們來看看代碼

新建一個activity 在manifest聲明 然後設置Theme爲自己寫的style 我的叫”DialogStyle” 繼承於@android:style/Theme.Dialog
設置屬性

<style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        //設置背景爲透明
        <item name="android:windowNoTitle">true</item>
      //  設置無標題
        <item name="android:backgroundDimEnabled">true</item>
        //  此屬性可以翻譯爲 是否允許對話框的背景變暗?如果允許背景就變暗了。
    </style>

彈出的activity的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/bubble"
        android:text="@string/hello_world" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="94dp"
        android:layout_marginLeft="26dp"
        android:orientation="vertical" >
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="183dp"
        android:text="我是對話框"
        android:textSize="30sp" />

</RelativeLayout>

如果要實現自定義dialog也可以這樣呦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章