控制對話框風格的activity的顯示大小與位置


  項目開發的需要,因為到現在項目接近完工,用戶提出對條件篩選方式進行修改,為做到最小的改動實現用戶的需求,各種百度,對於對話框風格大家普遍使用PopupWindow,但由於之前開發設計時使用的是activity對話框方式,所以今天就為大家介紹一下,如何通過activity實現與PopupWindow相同的效果,廢話不多講現在開始干貨。

  實現對話框風格的activity,我們需要在AndroidManifest.xml添加一句樣式聲明:

<activity
  android:name=".product.MyselfPayProduct"
   android:screenOrientation="portrait"
   android:theme="@android:style/Theme.Dialog" >

  不過這樣的對話框風格往往無法滿足我們的需要,顯示的效果不那么令人滿意,第一點就是如何控制對話框的大小:

//窗口對齊屏幕寬度
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.TOP;//設置對話框置頂顯示
win.setAttributes(lp);

  將這個控制語句添加在我們的對話框activity的onClick()方法中,這樣我們的對話框就可以寬度與屏幕一樣寬了,lp.gravity = Gravity.TOP;//設置對話框置頂顯示,android默認對話框居中顯示,我們可以通過這句代碼設置對話框的顯示位置。

  到這里是不是已經達到你的滿意了呢?下面在給大家介紹一下,如何通過activity實現微信右上角點擊加號的顯示效果。做這個顯示效果,我們需要通過在布局文件中通過android:layout_marginTop="50dp"這樣來調整對話框的位置,Android默認彈出框效果非常難看,為了達到更好的顯示效果,我們這里添加一個顯示的動畫效果:

進入動畫:

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

    <scale
        android:fromXScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXScale="1.0" 
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:duration="200"
        android:pivotX="0"
        android:pivotY="10%"
        />

</set>

退出動畫:

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

    <scale
        android:fromXScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXScale="1.0" 
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:duration="200"
        android:pivotX="0"
        android:pivotY="10%"
        />

</set>

  android動畫文件一般置於res的anim文件夾下,默認該文件夾不存在,需要我們手動添加。

  下面我們需要把我們的動畫添加的android的樣式文件:style.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        
    </style>
    
    <!-- 沒有標題 -->
    <style name="notitle" parent="AppBaseTheme">
         <item name="android:windowNoTitle">true</item>
    </style>
    
    <!-- 類似對話框效果 -->
    <style name="MyDialogTopRight">  
        <item name="android:windowBackground">@android:color/transparent</item>  
        <item name="android:windowIsTranslucent">true</item>  
        <item name="android:windowNoTitle">true</item>  
        <item name="android:windowAnimationStyle">@style/Anim_scale</item>  
    </style>  
 
    <style name="Anim_scale" parent="@android:style/Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/scale_in</item>
        <item name="android:activityOpenExitAnimation">@anim/scale_out</item>
        <item name="android:activityCloseEnterAnimation">@anim/scale_in</item>
        <item name="android:activityCloseExitAnimation">@anim/scale_out</item>
    </style>
    
</resources>

  最后我們需要修改一下我們在AndroidManifest.xml文件中的聲明:

android:theme="@style/MyDialogTopRight"

  到這里我們就完美實現了activity的對話框風格顯示。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM