【问题标题】:WindowManager addView() not respecting MATCH_PARENTWindowManager addView() 不尊重 MATCH_PARENT
【发布时间】:2018-04-07 12:47:51
【问题描述】:

我在使用 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));

【问题讨论】:

    标签: java android android-layout android-linearlayout android-windowmanager


    【解决方案1】:

    经过反复试验,我发现我没有将 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);
    

    【讨论】:

    • 即使这是您自己的答案,也请接受它作为正确答案,因为我相信这是一个正确答案。
    • 抱歉忘记了。现在完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2011-04-30
    • 2021-04-15
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多