Android開發小技巧——用Activity打造Dialog

wx67w2g4 8年前發布 | 12K 次閱讀 Activity Android開發 移動開發

前言

對話框是個好東西,創建簡單有實用。當下的開發中,很多的開發者反而更喜歡使用activity來代替對話框,至少筆者的團隊中,類似于升級提示或者指示頁及其他一些交互的地方,大量的把Dialog替換成activity,好處是顯而易見的,activity具有更靈活的操作和布局,另外很重要一點是,一些容易涉及內存泄漏的代碼放在activity中執行比放在Dialog中執行要好的多,當然這是筆者自己的觀點,文中有不對的地方,歡迎大家提出指正,好讓筆者及時改正,共同學習。

step1

先上效果圖:

Paste_Image.png

這個對話框常常能在一些APP中遇到,首先是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:layout_gravity="center"
    android:background="@drawable/popup_bg"
    android:orientation="vertical"
    tools:context="com.yankee.september_2.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_update"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Version"
            android:textColor="#53BAF8"
            android:textSize="14sp" />
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="12dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Nowadays, when it comes to the issues of robots, individuals’ opinions vary from person to person。 Some people believe that robots will enlighten our life, while other are worried about that they will ruin the whole world。 As I see, the increasing number of robots has the power to alter everything in the coming future entirely。"
                android:textColor="#404040"
                android:textSize="12sp" />
        </ScrollView>
    </LinearLayout>
    <Button
        android:id="@+id/btn_update"
        android:layout_width="130dp"
        android:layout_height="32dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Update now"
        android:textColor="#FFFFFF"
        android:textSize="14sp" />
</RelativeLayout>

布局代碼中需要注意的是,這個布局的大小是寫死的,因為從代碼中可以看出,對話框的鏤空效果其實是一張背景圖產生的,辛苦美術但是爽了程序員。

step2

接著就是在manifest文件的操作了,把對應的activity的theme設置為自定義的主題即可,背景設為透明,去掉標題,注意,這個主題繼承自對話框的主題。

manifest:

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:theme="@style/TipDialog">
</activity>

styles.xml:

<style name="TipDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

最后就沒有最后了,還有一點,就是讓當前這個activity繼承自Activity,而不是AppCompatActivity,否則會報主題的錯誤。

 

來自:http://www.jianshu.com/p/82d351192b36

 

 本文由用戶 wx67w2g4 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!