Android 如何打开FloatingView(库)并在其中显示自定义布局?
我正在应用程序中使用库 我已经成功地启动了它,现在我想要的是,当它被点击时,我想在它里面显示一个自定义的布局,就像在facebook messenger聊天头中发生的那样 以下是Android 如何打开FloatingView(库)并在其中显示自定义布局?,android,android-layout,android-service,floating,Android,Android Layout,Android Service,Floating,我正在应用程序中使用库 我已经成功地启动了它,现在我想要的是,当它被点击时,我想在它里面显示一个自定义的布局,就像在facebook messenger聊天头中发生的那样 以下是ChatHeadService.java文件的代码: public class ChatHeadService extends Service implements FloatingViewListener { private static final String TAG = "ChatHeadService
ChatHeadService.java
文件的代码:
public class ChatHeadService extends Service implements FloatingViewListener {
private static final String TAG = "ChatHeadService";
private IBinder mChatHeadServiceBinder;
private FloatingViewManager mFloatingViewManager;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mFloatingViewManager != null) {
return START_STICKY;
}
final DisplayMetrics metrics = new DisplayMetrics();
final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
mChatHeadServiceBinder = new ChatHeadServiceBinder(this);
final LayoutInflater inflater = LayoutInflater.from(this);
final ImageView iconView = (ImageView) inflater.inflate(R.layout.widget_chathead, null, false);
iconView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Charhead clicked");
}
});
mFloatingViewManager = new FloatingViewManager(this, this);
mFloatingViewManager.setFixedTrashIconImage(R.drawable.ic_trash_fixed);
mFloatingViewManager.setActionTrashIconImage(R.drawable.ic_trash_action);
final FloatingViewManager.Options options = new FloatingViewManager.Options();
options.shape = FloatingViewManager.SHAPE_CIRCLE;
options.overMargin = (int) (16 * metrics.density);
mFloatingViewManager.addViewToWindow(iconView, options);
// startForeground(NOTIFICATION_ID, createNotification());
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
destroy();
super.onDestroy();
}
private void destroy() {
if (mFloatingViewManager != null) {
mFloatingViewManager.removeAllViewToWindow();
mFloatingViewManager = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onFinishFloatingView() {
stopSelf();
}
public static class ChatHeadServiceBinder extends Binder {
/**
* FloatingRateService
*/
private final WeakReference<ChatHeadService> mService;
ChatHeadServiceBinder(ChatHeadService service) {
mService = new WeakReference<>(service);
}
public ChatHeadService getService() {
return mService.get();
}
}
}
请让我知道如何做我想做的事
很抱歉,问题的格式不正确。我还是个初学者
startService(new Intent(MainActivity.this, ChatHeadService.class));