Android 自定義Activity 實(shí)現(xiàn)懸浮 Dialog

轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://www.itdecent.cn/u/a5ad093cffe8

實(shí)現(xiàn)懸浮的自定義窗體有很多方法,自定義Dialog,自定義layout 均可以實(shí)現(xiàn)。自定義activity也是可以的,今天我就介紹一下activity的實(shí)現(xiàn)方法。


效果圖

如圖可以看出,當(dāng)前的窗體,第一眼其實(shí)和dialog沒(méi)有什么區(qū)別,但是它是自定義activity實(shí)現(xiàn)。如何實(shí)現(xiàn)的呢?
代碼如下:
新建activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.dialog_show_display_mode);
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.height = (int) (display.getHeight() * 0.3);
//        params.width = (int) (display.getWidth() * 0.8);
        params.alpha = 1.0f;
        getWindow().setAttributes(params);
        getWindow().setGravity(Gravity.CENTER);
}

layout樣式布局:

<?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="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:text="語(yǔ)音球開啟方式"
            android:textColor="@color/bg_black"
            android:textSize="20dp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/forget_password" />

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

        <LinearLayout
            android:id="@+id/display_dialog_intelligence_linner"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/display_dialog_intelligence_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/abc_btn_radio_to_on_mtrl_000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:text="智能顯示"
                android:textColor="@color/colorGreen2" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/forget_password" />

        <LinearLayout
            android:id="@+id/display_dialog_custom_linner"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/display_dialog_custom_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/abc_btn_radio_to_on_mtrl_000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:text="自定義顯示"
                android:textColor="@color/colorGreen2" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

style樣式:

<style name="activityTheme" parent="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
        <item name="android:windowFrame">@null</item><!--邊框-->
        <item name="android:windowIsFloating">true</item><!--是否浮現(xiàn)在activity之上-->
        <item name="android:windowIsTranslucent">false</item><!--半透明-->
        <item name="android:windowNoTitle">true</item><!--無(wú)標(biāo)題-->
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->
        <item name="android:backgroundDimEnabled">true</item><!--模糊-->
    </style>

資源文件配置activity:

<activity
            android:name=".__activity.FloatWindowDialogActivity"
            android:screenOrientation="portrait"
            android:theme="@style/activityTheme" />

ok 搞定。
喜歡的可以點(diǎn)贊,微信掃一掃關(guān)注我的公眾號(hào):

微信公眾號(hào)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評(píng)論 25 709
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,161評(píng)論 22 665
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,330評(píng)論 0 17
  • 開門,開燈 黃昏,星辰 關(guān)門,關(guān)燈 沉重而厚實(shí)的響聲 蕩在燈光昏暗的樓道 一圈,又一圈┈ 見慣的車往人去 霓虹閃爍...
    槿年dyt閱讀 247評(píng)論 4 3
  • 學(xué)會(huì)提問(wèn)(原書第10版) 讀書筆記 前言 很多人不知道怎樣切實(shí)有效地提出一些關(guān)鍵問(wèn)題 這本書的目標(biāo)是傳授批判性的提...
    鳧水閱讀 4,066評(píng)論 0 7

友情鏈接更多精彩內(nèi)容