將Android Activity以對話框Dialog形式顯示

jopen 11年前發布 | 61K 次閱讀 Android Android開發 移動開發

AndroidManifest.xml 文件中,申明 activity 的主題使用自定義對話框樣式。
<activity android:name=".SensorGetValueActivity"
android:label="@string/activity_custom_dialog"
android:theme="@style/Theme.CustomDialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>

2、res/values/styles.xml 樣式文件中定義一個對話框主題樣式,這里繼承了 android:style/Theme.Dialog 主題,

并且窗口樣式 android:windowBackground 引用了 @drawable/filled_box

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/filled_box</item>
</style>

3、res/drawable/filled_box.xml 定義了 Shape 類型的 drawable(抽象的可畫區域),最終是通過這個來實現新對話框的樣式。
<shape xmlns:android=";
<solid android:color="#f0600000"/>
<stroke android:width="3dp" color="#ffff8080"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape> </pre>

備注:如果出現不全屏的問題,可以通過設置layout文件:android:minWidth="2400dip" 解決

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