我正在尝试使Facebook Messanger聊天头喜欢的应用程序,可以显示聊天头从服务。我读过很多例子,他们都使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT。但问题是它阻止了所有其他应用程序的输入,所以用户将无法移动屏幕,打开应用程序等。然后我尝试使用WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,它显示聊天头,我可以在屏幕上自由导航,但现在聊天头不会收到任何输入,如onClick。
所以我的问题是,如何让聊天头不会阻塞其他应用程序的输入,同时它仍然可以接收onClick或onTouch等输入。
下面是我如何在服务的onCreate内部创建视图:
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
view = LayoutInflater.from(this).inflate(R.layout.chat_popup, null);下面是我在服务中显示视图的方式:
private void showBubble() {
try {
mWindowManager.removeView(view);
} catch (Exception ignore) {
ignore.printStackTrace();
} finally {
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mWindowManager.addView(view, params);
trashView.setVisibility(View.GONE);
isViewAdded = true;
}
}下面是我的聊天头布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical">
<RelativeLayout
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/text_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/profile_imageview"
android:layout_marginBottom="5dp"
android:layout_marginEnd="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:elevation="20dp"
android:minHeight="50dp"
android:minWidth="50dp"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="25dp">
<TextView
android:id="@+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:gravity="start"
android:maxLines="5"
android:paddingBottom="5dp"
android:paddingEnd="15dp"
android:paddingStart="55dp"
android:paddingTop="5dp"
android:scrollbars="vertical"
android:textColor="@color/white"
android:textSize="16sp" />
</android.support.v7.widget.CardView>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_imageview"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:elevation="50dp"
android:src="@drawable/thumbnail" />
</RelativeLayout>
<android.support.v7.widget.CardView
android:id="@+id/trash_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:cardBackgroundColor="@color/transparentBlack"
app:cardCornerRadius="30dp">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="20dp"
android:padding="8dp"
android:src="@drawable/ic_close_white_24dp" />
</android.support.v7.widget.CardView>
</RelativeLayout>我知道有解决方案,但现在我被困住了,我一直在寻找解决方案,但没有运气。
有人能帮上忙吗?
感谢您的支持。
发布于 2017-03-02 15:04:55
使用此代码表示WindowManager的大小
int HW = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (pt number as dp), this.getResources().getDisplayMetrics());
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
HW,//width
HW,//height
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);您当前的代码设置为MATCH_PARENT,因此它将阻止该窗口下的所有内容。此外,您还必须定义ViewGroup,然后访问它的元素,然后执行onClickListener。我有完整的教程,请查看this tutorial
viewgroup = (ViewGroup)vG.findViewById(R.id.widget);
relativeview = (RelativeLayout)vG.findViewById(R.id.whole_widget_view);
btn = (Button)vG.findViewById(R.id.btn);
btn.setOnClickListener(...)https://stackoverflow.com/questions/42548790
复制相似问题