在窗口管理器中膨胀类时出错

时间:2018-09-03 07:44:39

标签: java android xml android-studio android-activity

代码

public class FloatingWindow extends Service{
WindowManager wm;
LinearLayout ll;
LayoutInflater li;

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

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    View myview;
    li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.TYPE_INPUT_METHOD |
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
    params = new WindowManager.LayoutParams(
            500, 1000, WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.CENTER | Gravity.CENTER;
    myview = li.inflate(R.layout.service_floating_window, null); // your layout here
    wm.addView(myview, params);

XML

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


<RelativeLayout
    android:background="@drawable/allusers"
    android:layout_below="@id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/top"
    android:padding="2dp"
    android:paddingStart="20dp"
    android:paddingEnd="10dp"
    android:background="@color/black"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <ImageButton
        android:background="@null"
        android:src="@drawable/close"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />



    <LinearLayout
        android:paddingVertical="5dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <TextView
            android:text="Name"
            android:layout_centerVertical="true"
            android:id="@+id/name"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"/>

        <TextView
            android:text="Online"
            android:layout_centerVertical="true"
            android:id="@+id/online"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12dp"/>

    </LinearLayout>

</RelativeLayout>





    <android.support.v7.widget.RecyclerView
        android:layout_above="@+id/linearlayout"
        android:scrollbars="vertical"
        android:id="@+id/message_list"
        android:padding="2dp"
        android:background="@drawable/allusers"
        android:layout_below="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

<RelativeLayout
    android:background="@null"
    android:padding="1dp"
    android:layout_alignParentBottom="true"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <ImageButton
        android:scaleType="center"
        android:src="@drawable/add_image"
        android:id="@+id/add_image"
        android:layout_width="50dp"
        android:background="@null"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />

    <View

        android:layout_marginVertical="5dp"
        android:layout_toRightOf="@id/add_image"
        android:id="@+id/midleft"
        android:layout_width="0.5px"
        android:layout_height="match_parent"
        android:background="@color/white"/>

    <EditText
        android:paddingVertical="5dp"
        android:paddingHorizontal="10dp"
        android:textColorHint="@color/white"
        android:hint="Write a message..."
        android:id="@+id/input_message"
        android:textColor="@color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@+id/add_image"
        android:layout_toStartOf="@+id/send_message" />

    <View
        android:layout_marginVertical="5dp"
        android:layout_toLeftOf="@id/send_message"
        android:id="@+id/midright"
        android:layout_width="0.5px"
        android:layout_height="match_parent"
        android:background="@color/white"/>


    <ImageButton
        android:scaleType="center"
        android:background="@null"
        android:src="@drawable/send_button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/send_message"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:text="Send" />

    <View
        android:layout_marginHorizontal="10dp"
        android:layout_alignParentBottom="true"
        android:id="@+id/bottomline"
        android:layout_width="match_parent"
        android:layout_height="0.5px"
        android:background="@color/white"/>

</RelativeLayout>
</RelativeLayout>
 </RelativeLayout>

enter image description here

每当我尝试调出浮动窗口时,我都会收到此异常。我对窗口管理器也很陌生,因此不确定导致问题的原因...我了解到的一件事是xml文件无法具有appbar。 。但是我的没有但仍然显示错误...有人可以帮我吗...

1 个答案:

答案 0 :(得分:0)

好的,问题出在这里

android:layout_height="?attr/actionBarSize"

我真的不得不'注释'每一行以查看问题出在哪里,因为其他地方都没有问题

我将高度设置为50dp,现在工作正常