想必大家都用過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裡面的控件進行操作