将Activity设置成窗口模式有两种方法,一种是利用系统提供的theme在清单配置文件中设置当前Activity的theme属性。另一种就是根据项目的需求自定义主题。


第一种方法:

在AndroidManifest.xml清单配置文件中修改当前Activity的theme属性

android:theme="@style/Theme.AppCompat.Light.Dialog"

完整的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.zwb.floatactivity">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/Theme.AppCompat.Light.Dialog">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

运行效果图:


第二种方法就是根据项目中的需求,自定义主题

styles.xml

<resources>    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>    <style name="Theme.FloatActivity" parent="Theme.AppCompat.Light.Dialog">        <item name="android:windowBackground">@drawable/float_box</item>    </style></resources>
其中Theme.FloatActivity就是我们自定义的属性。在drawable资源文件夹下新建一个样式文件,来设置我们自定义主题的样式

float_box.xml:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="@color/colorAccent"/>    <stroke android:width="3dp" android:color="#000000"/>    <corners android:radius="5dp"/>    <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/></shape>

最后,在清单配置文件中,将当前的Activity的theme属性设置为我门自定义的theme就可以了

 android:theme="@style/Theme.FloatActivity"
自定义的运行效果如下:



更多相关文章

  1. Bitmap recycle()
  2. Android(安卓)安卓动画 属性动画 - 组合动画
  3. Android的绘制详解(Canvas、Paint、Path等)
  4. LinearLayout中的layout_weight属性
  5. ScrollView 的一些属性
  6. Android(安卓)7.0 增加以太网设置DHCP和静态IP
  7. android 编程小技巧(持续中)
  8. 《第一行代码》阅读笔记(三十四)——Material Design
  9. Android的页面管理控件ViewFlipper

随机推荐

  1. 20201203 装饰器之高阶函数
  2. 计算机网络中这些高频考题,你还在死记硬背
  3. flex项目的三个属性及商城页眉页脚布局
  4. php pdo 插入中文出现乱码解决 办法
  5. 基于 Flink 的实时特征平台在携程的应用
  6. Apache Kafka 2.0.0 正式发布,多项重要功
  7. 5大Top经济刊物的在40年里发生了什么?
  8. 一篇文章搞清楚什么是分布式系统 CAP 定
  9. gstvideo_has_codec: none can handle '
  10. gstvideo_has_codec: none can handle '