【问题标题】:Floating widgets/pop up window in androidandroid中的浮动小部件/弹出窗口
【发布时间】:2014-12-31 05:31:30
【问题描述】:

我正在开发一个应用程序,在该应用程序中,当收到呼叫时应该出现一个浮动组件,并且该组件将有几个按钮来执行必要的操作。 我已经尝试了以下。 我通过使主要活动半透明来实现一个弹出窗口。当这个组件弹出时,我可以在屏幕上移动它,但由于活动是半透明的,我无法执行任何other 活动。

在这里你可以看到弹出窗口,我可以移动它,但我不能在后台滚动菜单抽屉。我怎样才能以一种可以同时执行两种操作的方式实现,即在弹出窗口和背景屏幕上。

我的代码

`public class MainActivity extends Activity {
    int mCurrentX;
    int mCurrentY;
     private float mDx;
     private float mDy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         final View cv = new View(this);


            TextView tv = new TextView(this);
            tv.setBackgroundColor(0xffeeeeee);
            tv.setTextColor(0xff000000);
            tv.setTextSize(24);
            tv.setText("click me\nthen drag me");
            tv.setPadding(8, 8, 8, 8);
          final PopupWindow  mPopup = new PopupWindow(tv, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            OnTouchListener otl = new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    int action = event.getAction();

                    if (action == MotionEvent.ACTION_DOWN) {
                        mDx = mCurrentX - event.getRawX();
                        mDy = mCurrentY - event.getRawY();
                    } else
                    if (action == MotionEvent.ACTION_MOVE) {
                        mCurrentX = (int) (event.getRawX() + mDx);
                        mCurrentY = (int) (event.getRawY() + mDy);
                        mPopup.update(mCurrentX, mCurrentY, -1, -1);

                    }
                    return true;
                }
            };
            tv.setOnTouchListener(otl);

            mCurrentX = 20;
            mCurrentY = 50;

            cv.post(new Runnable() {
                @Override
                public void run() {
                    mPopup.showAtLocation(cv, Gravity.NO_GRAVITY, mCurrentX, mCurrentY);
                }
            });
    }

}`

清单

`<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

        <activity android:name=".MainActivity"
                  android:theme="@android:style/Theme.Translucent.NoTitleBar"
                  android:label="@string/app_name"
                  android:configChanges="orientation|screenSize"
                  android:windowSoftInputMode="adjustResize|stateAlwaysHidden">

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

        </activity>

               <activity
                android:name="PopupMainActivity"
                android:label="@string/app_name"
                android:theme="@style/Theme.FloatingWindow.Popup"
                android:configChanges="orientation|screenSize"
                android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
                android:clearTaskOnLaunch="true"
                android:exported="true"
                tools:ignore="ExportedActivity" />

    </application>`

请帮助我。我想总体上实现一种小部件类型的组件。谢谢!!

【问题讨论】:

    标签: android popupwindow


    【解决方案1】:

    您必须启动一个前台服务并使用 WindowManager 在其他所有内容之上进行绘制,管理您的弹出位置、大小等...

    WindowManger windowManager = Context.getSystemService(Context.WINDOW_SERVICE);
    

    您还必须将此权限“android.permission.SYSTEM_ALERT_WINDOW”添加到您的清单中。

    一个更简单的解决方案是使用一个名为 StandOut 的库,它基本上处理了我上面提到的所有内容并提供了额外的功能,例如:

    • 窗口装饰器(标题栏、最小化/关闭按钮、边框、调整大小手柄)
    • Windows 可移动并可调整大小。你可以把它带到前面,最小化, 并关闭
    • 可以恢复最小化的窗口(示例 APK 使用 通知面板)
    • 创建多种类型的窗口,以及每种类型的多个窗口

    【讨论】:

    • 感谢您的回答,请详细说明如何使用该库?我是 android 新手,正在学习一些概念,我可能需要更多帮助!谢谢!
    • 查看库示例项目,特别是 StandOutExampleActivity.java、SimpleWindow.java 和 AndroidManifest.xml
    • @MostafaGazar 当 StandOut 库显示 CommingSoon 时?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    相关资源
    最近更新 更多