簡體   English   中英

WindowManager addView()不遵守MATCH_PARENT

[英]WindowManager addView() not respecting MATCH_PARENT

使用WindowManager將布局顯示為疊加層時出現問題。 布局僅覆蓋足夠的區域以顯示其子視圖覆蓋的最小的最小區域,而不是全屏顯示。

我正在使用以下代碼來使用WindowManager顯示布局:

    LinearLayout linearLayout = new LinearLayout(MainActivity.this);
    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    View inflatedLayout = inflater.inflate(R.layout.note_taking_layout, null, false);
    linearLayout.addView(inflatedLayout);

final WindowManager.LayoutParams paramsForOverlay = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, // this allows it to show over lock screen AND be able to receive touch.
                    FLAG_TRANSLUCENT_STATUS | FLAG_NOT_TOUCH_MODAL,
                    PixelFormat.TRANSLUCENT);
            paramsForOverlay.gravity = Gravity.TOP | Gravity.CENTER;


    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(new DisplayMetrics());

if (windowManager != null) {
                linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                windowManager.addView(linearLayout, paramsForOverlay);
            }

這是R.layout.note_taking_layout的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">

<LinearLayout
    android:id="@+id/llCanvas"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <TextView
        android:layout_gravity="center"
        android:text="ABCD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:layout_weight="1"
    android:background="@color/colorPrimaryDark"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="0dp">

    <TextView
        android:textColor="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="world" />

    <Button
        android:id="@+id/bDestroy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello" />
</LinearLayout>

這是它的外觀:

在此處輸入圖片說明

這實際上是這樣的:

在此處輸入圖片說明

我該怎么做才能覆蓋整個屏幕?

編輯:我顯示一個按鈕,並使用此按鈕將其擴展到全屏:

final Button button = new Button(MainActivity.this);
button.setText("tis a button");
button.setLayoutParams(
                    new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));

經過反復試驗,我發現我沒有將MATCH_PARENT設置為View inflatedLayout。 所以代替這個:

            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View inflatedLayout = inflater.inflate(R.layout.note_taking_layout, null, false);
            linearLayout.addView(inflatedLayout);

我應該這樣做:

            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View inflatedLayout = inflater.inflate(R.layout.note_taking_layout, null, false);
            inflatedLayout.setLayoutParams(     // THIS!
                    new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
            linearLayout.addView(inflatedLayout);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM