Android實現可使用自訂透明Dialog樣式的Activity完整執行個體_Android

來源:互聯網
上載者:User

本文執行個體講述了Android實現可使用自訂透明Dialog樣式的Activity。分享給大家供大家參考,具體如下:

有時你需要一個對話方塊,但同時對話方塊中的內容有更多控制和能控制其生命週期,這時你可以使用帶有Dialog樣式的Activity來應用你的項目中,想使Activity有對話方塊那樣效果可以在Androidmanifest中添加 Android:style/Theme.Dialog 的主題特性

例如這樣:

<activity android:name="MyDialogActivity"    android:theme="@android:style/Theme.Dialog"></activity>

如果你覺得上面不足以滿足你的需求想實現 半透明的, 圓角的,不妨可以試試自訂對話方塊的style

首先

第一步: 準備工作要做好 一張.9圖片


loading_bg.9.png

第二步 :自訂style   在你項目 values 檔案夾中建立一個styles.xml 檔案

接著在styles檔案中添加 名為 MyDialogStyle的style

<style name="MyDialogStyle">    <item name="android:windowBackground">@android:color/transparent</item>    <item name="android:windowFrame">@null</item>    <item name="android:windowNoTitle">true</item>    <item name="android:windowIsFloating">true</item>    <item name="android:windowIsTranslucent">true</item>    <item name="android:windowContentOverlay">@null</item>    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>    <item name="android:backgroundDimEnabled">true</item></style>

第三步:Dialog的布局 在layout檔案夾中添加布局檔案  activity_main.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">  <RelativeLayout    android:layout_width="180dp"    android:layout_height="180dp"    android:layout_centerInParent="true"    android:background="@drawable/loading_bg" >    <LinearLayout      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:layout_alignParentLeft="true"      android:layout_alignParentTop="true"      android:gravity="center"      android:orientation="vertical" >      <ProgressBar        android:id="@+id/progressBar1"        style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal" />      <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="正在登入"        android:textColor="#fff"        android:textSize="20sp" />    </LinearLayout>  </RelativeLayout></RelativeLayout>

建立 DialogActivity類

public class DialogActivity extends Activity{  @Override  protected void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    // 這裡你可以進行一些等待時的操作,我這裡用8秒後顯示Toast代理等待操作    new Handler().postDelayed(new Runnable(){      @Override      public void run(){        DialogActivity.this.finish();        Toast.makeText(getApplicationContext(), "登入成功", Toast.LENGTH_SHORT).show();      }    }, 8000);  }}

第四步 : 在Manifest檔案中對DialogActivity進行註冊,並使用上面自訂MyDialogStyle樣式

<activity   android:name="LoadingActivity"   android:theme="@style/MyDialogStyle" />

第五步:運行工程並查看效果圖

打完收工

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android控制項用法總結》、《Android視圖View技巧總結》、《Android檔案操作技巧匯總》、《Android操作SQLite資料庫技巧總結》、《Android操作json格式資料技巧總結》、《Android資料庫操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》及《Android資源操作技巧匯總》

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.