gpt4 book ai didi

android - 可以在服务中扩展 XML 布局吗? (安卓)

转载 作者:IT老高 更新时间:2023-10-28 23:21:58 26 4
gpt4 key购买 nike

我想在服务中扩展 xml 布局。你怎么做到这一点?我尝试使用充气机和窗口管理器,但我认为它不起作用。这有什么战术吗?谢谢。

<?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>

这是我要显示的 xml 的服务代码。

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");


}
}

最佳答案

可以在服务上放一个图形代码是这样的:

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);

关于android - 可以在服务中扩展 XML 布局吗? (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19539887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com