从 android.support.design.widget.FloatingActionButton 膨胀浮动操作按钮

Inflate Floating Action Button from android.support.design.widget.FloatingActionButton

(项目概览部分)

我想在所有其他应用程序和系统图标上做一个飞行按钮。按下它后,会弹出一个菜单,您可以从中选择多个选项。按下并移动按钮后,您可以将其移动到屏幕上的任何位置。

到目前为止,我在一个项目中有屏幕上的飞行按钮(实际上是飞行的 bmp 文件),在第二个项目中有带有弹出菜单的浮动按钮。

我现在要做的是把bmp文件替换成真正的带菜单的浮动按钮。

我尝试这样做的方式:

(最重要的从这里开始):)

我通过addView方法给windowManager添加了飞行按钮。 Flyingbutton 是一个 View 对象。这就是我来收割机的地方。

我试图执行如何在服务 class 中通过 id 查找视图,我通过膨胀 android.support.design.widget.FloatingActionButton class 来做到这一点 但现在我看到 inflate 方法不适合 class.

我在 Stack Overflow 上看到的帖子很少,但 none 对我有帮助。

有什么想法吗?

也许你会看到到目前为止背景不是透明的,但这不是目前的问题。 :)

logcat 说...

FATAL EXCEPTION: main

Process: com.example.komp.floatingbuttonpowtorka, PID: 32452

java.lang.RuntimeException: Unable to create service com.example.komp.floatingbuttonpowtorka.FlyingButton: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2780)
    at android.app.ActivityThread.access00(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5333)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.example.komp.floatingbuttonpowtorka.FlyingButton.onCreate(FlyingButton.java:63)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2770)
    at android.app.ActivityThread.access00(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
    at android.os.Handler.dispatchMessage(Handler.java:110) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:5333) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
    at com.example.komp.floatingbuttonpowtorka.FlyingButton.onCreate(FlyingButton.java:63) 
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2770) 
    at android.app.ActivityThread.access00(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
    at android.os.Handler.dispatchMessage(Handler.java:110) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:5333) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
    at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:110)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:104)
    at java.lang.reflect.Constructor.constructNative(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
    at com.example.komp.floatingbuttonpowtorka.FlyingButton.onCreate(FlyingButton.java:63) 
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2770) 
    at android.app.ActivityThread.access00(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
    at android.os.Handler.dispatchMessage(Handler.java:110) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:5333) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
    at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        startService(new Intent(MainActivity.this, FlyingButton.class));
        finish();
}

@Override
protected void onResume() {
    Bundle bundle = getIntent().getExtras();

    if(bundle != null && bundle.getString("LAUNCH").equals("YES")) {
        startService(new Intent(MainActivity.this, FlyingButton.class));
    }
    super.onResume();
}
}

FlyingButton.java

public class FlyingButton extends Service {

private WindowManager windowManager;
private ImageView chatHead;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);

    chatHead.setImageResource(R.drawable.floating);


    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.activity_main, null);

    FloatingActionButton fab = (FloatingActionButton) layout.findViewById(R.id.fab);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager.addView(fab, params);

    try {
        chatHead.setOnTouchListener(new View.OnTouchListener() {
            private WindowManager.LayoutParams paramsF = params;
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        // Get current time in nano seconds.

                        initialX = paramsF.x;
                        initialY = paramsF.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                        paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(chatHead, paramsF);
                        break;
                }
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null) windowManager.removeView(chatHead);
}

}

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@drawable/filter"
    app:backgroundTint="@color/colorAccent"
    xmlns:android="http://schemas.android.com/apk/res/android" />
</RelativeLayout>

styles.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.komp.floatingbuttonpowtorka"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat">
    <application android:name=".ApplicationContextProvider"
        android:label="@string/app_name"/>
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<service
    android:name="com.example.komp.floatingbuttonpowtorka.FlyingButton"
    android:exported="true" />
</application>

</manifest>

gradle 应用插件:'com.android.application'

android{ 编译SDK版本23 buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.example.komp.floatingbuttonpowtorka"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}

好像是活动定了主题,服务没有定主题。 要以编程方式指定一个,请将其添加到服务的 onCreate 方法中。

setTheme(R.style.AppTheme);

它会修复您的特定错误,但该应用程序不会 运行 按原样。

1.

我不得不改变

windowManager.addView(fab, params);windowManager.addView(layout, params);

2.

如果 targetSdkVersion 设置为 23 或更高,应用程序将不会 运行。 (在 build.gradle 文件中设置 targetSdkVersion 22This answer will maybe help


一些注意事项:

1.

您没有使用在 styles.xml

中声明的样式

AndroidManifest.xml中,需要将android:theme="@style/Theme.AppCompat"改为android:theme="@style/AppTheme"

仅将其设置为 <Application> 会将其应用于所有活动。

2.

在你的AndroidManifest.xml

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18" />

可以删除,因为此信息已在 build.gradle 文件中指定。