Android項目實戰(七):Dialog主題Activity實現自訂對話方塊效果

來源:互聯網
上載者:User

標籤:down   覆蓋   半透明   ica   實現   http   現在   決定   wan   

原文:Android項目實戰(七):Dialog主題Activity實現自訂對話方塊效果

想必大家都用過Dialog主題的Activity吧,用它來顯示自訂對話方塊效果絕對是一個非常不錯的選擇。

即把activity互動介面以Dialog的形式展現出來,Dialog主題的Activity大小將以內容的寬高來決定

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

可以看到設定為Theme.Dialog主題的activity顯示效果,

是類似對話方塊的形式顯示出來的,而背景則是這個Activity的上一個activity互動介面,

或者如果此Activity是程式第一個Activity,背景則是手機案頭

那麼讓我們自己做一個漂亮點的對話方塊形式的Activity

首先,要把Activity內建的標題去掉

使用 requestWindowFeature(Window.FEATURE_NO_TITLE); 語句

注意 需要在 setContentView(R.layout.main); 語句之前使用

畫一個布局,具體效果看自己的Xml寫的怎麼樣了

<?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裡面的控制項進行操作

Android項目實戰(七):Dialog主題Activity實現自訂對話方塊效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.