无法在WindowManager中设置ImageView的宽度

时间:2015-09-19 05:00:25

标签: android imageview android-windowmanager

我一直在为Android聊天头开发应用程序。 我已经创建了如下的服务。此服务由MainActivity启动。

public class StatusBarService extends Service {


private WindowManager windowManager;
private ImageView chatHead ;
int BUFFER = 60;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


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

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);





    chatHead = new ImageView(this);


    LinearLayout.LayoutParams vp =
            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);


    chatHead.setLayoutParams(vp);



    chatHead.setImageResource(R.drawable.white);


    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
           100,
         WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR  ,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;

    params.x = 0;
    params.y = 0;

    windowManager.addView(chatHead, params);

}

I am getting this .

我想设置相同的宽度。目前我无法做到这一点。

我尝试更改LayoutParams的大量参数。我觉得问题是imageview无法填满WindowManager视图。 请让我知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

请将视图包装在伪容器视图中(如衬里布局)。它对我有用。 :)