可能夸大了服务的XML布局? (安卓) [英] Possible to inflate an XML layout in a Service? (Android)

查看:147
本文介绍了可能夸大了服务的XML布局? (安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想夸大的服务的XML布局。你怎么做到这一点?我试图用打气筒和窗口管理器,但我认为它不工作。对此有何高招?谢谢。

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent机器人:layout_height =match_parent
机器人:方向=垂直
机器人:ID =@ + ID /主
>
    <的LinearLayout
        机器人:ID =@ + ID /顶
        机器人:layout_height =match_parent
        机器人:layout_width =match_parent
        机器人:layout_weight =1
        机器人:方向=横向
        >


    <的FrameLayout
        机器人:ID =@ + ID /左上
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent

        机器人:标签=左上
        机器人:layout_weight =1.0
        机器人:后台=#ff3d38>< /的FrameLayout>


    <的FrameLayout
        机器人:ID =@ + ID / topRight
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:标签=topRight

    机器人:layout_weight =1.0
        机器人:后台=#fff839>< /的FrameLayout>

    < / LinearLayout中>

<的LinearLayout
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent
    机器人:layout_weight =1
    机器人:方向=横向
    机器人:ID =@ + ID /底
    >

<的FrameLayout
        机器人:ID =@ + ID / BOTTOMLEFT
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent

        机器人:layout_weight =1.0
        机器人:后台=#1e22ff>

    < /的FrameLayout>

    <的FrameLayout
        机器人:ID =@ + ID / bottomRight
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent

        机器人:layout_weight =1.0
        机器人:后台=#32ff1f>< /的FrameLayout>
 

这是我的服务code要显示的XML。

 包com.toksis.pvscreen;

进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.graphics.PixelFormat;
进口android.os.IBinder;
。进口的Andr​​oid preference preferenceManager。
进口android.util.Log;
进口android.view.Gravity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.WindowManager;
进口android.widget.Button;
进口android.widget.FrameLayout;
进口android.widget.LinearLayout;


公共类DragDropButtonMainService延伸服务{
私人的LinearLayout为主;
私人的LinearLayout firstLayer;
私人的LinearLayout secondLayer;
私人的FrameLayout vTopLeft;
私人的FrameLayout vTopRight;
私人的FrameLayout vBottomLeft;
私人的FrameLayout vBottomRight;



私人按钮mainButton;
私人的ViewGroup MView的;
私人LayoutInflater充气;

公众的IBinder onBind(意向意图){
    返回null;
}


公共无效的onCreate(){
    super.onCreate();

    createDragDropLayout();



}



无效createDragDropLayout(){

   mainButton =新的按钮(这一点);
   mainButton.setText(主);


   WindowManager.LayoutParams PARAMS =新WindowManager.LayoutParams(
           WindowManager.LayoutParams.WRAP_CONTENT,
           WindowManager.LayoutParams.WRAP_CONTENT,
           WindowManager.LayoutParams.TYPE_PHONE,
           WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                   | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                   | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
           PixelFormat.TRANSLUCENT
   );




   窗口管理WM =(窗口管理器)getSystemService(WINDOW_SERVICE);

   充气=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   MVIEW =(ViewGroup中)inflater.inflate(R.layout.dragdroplayout,NULL);


   vTopLeft =(的FrameLayout)mView.findViewById(R.id.topLeft);
   vTopRight =(的FrameLayout)mView.findViewById(R.id.topRight);
   vBottomLeft =(的FrameLayout)mView.findViewById(R.id.bottomLeft);
   vBottomRight =(的FrameLayout)mView.findViewById(R.id.bottomRight);
   firstLayer =(的LinearLayout)mView.findViewById(R.id.top);
   secondLayer =(的LinearLayout)mView.findViewById(R.id.bottom);

   //主=(的LinearLayout)mView.findViewById(R.id.main);

  // main.addView(firstLayer);
  // main.addView(secondLayer);


 // firstLayer.addView(vTopLeft);
 // firstLayer.addView(vTopRight);

 // secondLayer.addView(vBottomLeft);
// secondLayer.addView(vBottomRight);

  // wm.addView(mainButton,则params);
     wm.addView(MVIEW,则params);

   Log.d(TOK,增加MVIEW);


   }
}
 

解决方案

有可能放置在服务一个图形的code是这样的:

  LI =(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        WM =(窗口管理器)getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams PARAMS =新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.gravity = Gravity.RIGHT | Gravity.TOP;
        MyView的= li.inflate(R.layout.traslucent,NULL);

        wm.addView(MyView的,则params);
 

I want to inflate an xml layout in a service. How do you do this? I tried to use inflater and Window Manager but I think it don't work. Any tactics on this? Thanks.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main"
>
    <LinearLayout
        android:id="@+id/top"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        >


    <FrameLayout
        android:id="@+id/topLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:tag="topLeft"
        android:layout_weight="1.0"
        android:background="#ff3d38"></FrameLayout>


    <FrameLayout
        android:id="@+id/topRight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="topRight"

    android:layout_weight="1.0"
        android:background="#fff839"></FrameLayout>

    </LinearLayout>

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:id="@+id/bottom"
    >

<FrameLayout
        android:id="@+id/bottomLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_weight="1.0"
        android:background="#1e22ff">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/bottomRight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_weight="1.0"
        android:background="#32ff1f"></FrameLayout>

This is my Service Code for the xml to be displayed.

package com.toksis.pvscreen;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


public class DragDropButtonMainService extends Service {
private LinearLayout main;
private LinearLayout firstLayer;
private LinearLayout secondLayer;
private FrameLayout  vTopLeft;
private FrameLayout  vTopRight;
private FrameLayout  vBottomLeft;
private FrameLayout  vBottomRight;



private Button mainButton;
private ViewGroup mView;
private LayoutInflater inflater;

public IBinder onBind(Intent intent) {
    return null;
}


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

    createDragDropLayout();



}



void createDragDropLayout(){

   mainButton = new Button(this);
   mainButton.setText("Main");


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




   WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

   inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   mView =  (ViewGroup) inflater.inflate(R.layout.dragdroplayout, null);


   vTopLeft   = (FrameLayout) mView.findViewById(R.id.topLeft);
   vTopRight  = (FrameLayout)  mView.findViewById(R.id.topRight);
   vBottomLeft = (FrameLayout) mView.findViewById(R.id.bottomLeft);
   vBottomRight= (FrameLayout) mView.findViewById(R.id.bottomRight);
   firstLayer = (LinearLayout) mView.findViewById(R.id.top);
   secondLayer = (LinearLayout) mView.findViewById(R.id.bottom);

   //    main = (LinearLayout) mView.findViewById(R.id.main);

  //        main.addView(firstLayer);
  //      main.addView(secondLayer);


 //    firstLayer.addView(vTopLeft);
 //           firstLayer.addView(vTopRight);

 //         secondLayer.addView(vBottomLeft);
//       secondLayer.addView(vBottomRight);

  // wm.addView(mainButton, params);
     wm.addView(mView,params);

   Log.d("tok", "add mview");


   }
}

解决方案

it is possible to place a graphic on the service the code is this:

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.gravity = Gravity.RIGHT | Gravity.TOP;
        myview = li.inflate(R.layout.traslucent, null);     

        wm.addView(myview, params);

这篇关于可能夸大了服务的XML布局? (安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆