如何用Android Studio制作浮动窗口应用程序

本文介绍了如何使用Android Studio制作浮动窗口应用程序。通过创建新项目、设置主题颜色、构建布局以及编写Java代码,实现了一个可以自由最小化和最大化的应用程序,提供了一种方便的用户交互方式。
摘要由CSDN通过智能技术生成

如何用Android Studio制作浮动窗口应用程序


一、前言

在台式电脑上,我们可以很轻松的最小化窗口,在后台做一些事情,并在任何时候都可以最大化窗口。但我们在android应用程序中看不到这一功能。如今,我们可以看到android提供了分屏功能,但这是操作系统提供的功能,而不是应用程序的单独功能。让我们制作一个应用程序,只需单击一个按钮即可将其最小化和最大化。该功能可以在很多方面帮助用户。假设你正在阅读一些带有数学计算的文档,那么最小化计算器将非常有用。


二、实现步骤

1.创建新项目

1.1要在Android Studio中创建新项目。请注意,先选择Emply Activity,再选择选择Java作为编程语言。

2.添加主题颜色

2.1为应用程序添加新颜色:

转到values->Colors.xml。可以在此处添加任何自定义颜色。我们添加了这两种颜色。

代码如下(示例):

<color name="gfgTheme">#FF2F8D46</color>
<color name="gfgThemeTwo">#FF098043</color>

​2.2移除操作栏:

在Android Studio 4.1中,转到values->themes。有两个主题XML文件,一个用于亮模式,另一个用于暗模式。在这两个XML中,在样式块中将父属性更改为Theme.MaterialComponents.DayNight.NoActionBar。

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.FloatingWindow" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

2.3更改应用程序的主主题颜色:

更改应用程序的主要主题颜色:在同一文件中,第一个项目块必须与应用程序的主要颜色有关。在这里添加了新添加的颜色。在项目块中添加@color/gfgTheme 或 @color/gfgThemeTwo.

3.制作Layouts

3.1制作activity_main.xml文件

此 XML 文件制作应用程序的主要活动的布局。布局没有那么复杂。在约束布局中只有一个Button、TextView、EditText和ConstraintLayout中的一个又一个按钮。下面是 XML 代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/buttonMinimize"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Wink"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/titleText"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:text="浮动窗口"
        android:textColor="@color/gfgTheme"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonMinimize" />

    <EditText
        android:id="@+id/descEditText"
        android:layout_width="match_parent"
        android:layout_height="330dp"
        android:layout_marginTop="10dp"
        android:gravity="start"
        android:hint="这里写下内容"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingRight="20dp"
        android:paddingBottom="10dp"
        android:textColor="@android:color/black"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleText" />

    <Button
        android:id="@+id/saveBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="保存"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/descEditText" />

</androidx.constraintlayout.widget.ConstraintLayout>

3.2制作floating_layout.xml文件

转到 res -> layout。右键单击layout -> New -> Layout Resource File。添加布局的名称(此处floating_layout)。此 XML 文件创建浮动窗口的布局。它具有与主布局相同的组件,但大小限制略有不同。下面是 XML 代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <Button
        android:id="@+id/buttonMaximize"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:background="@color/gfgThemeTwo"
        android:text="Wink"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/titleText"
        android:layout_width=
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值