android中的中心弹出窗口并不适用于所有手机

时间:2017-04-22 19:31:00

标签: android android-layout popup

在我的应用上,我有不同的弹出窗口。在我的手机和我的所有虚拟设备上,它们完全位于屏幕上。不幸的是,所有手机都不是这样。

我的java文件中的代码如下:

编辑:我已删除fitSystemWindows=true,但这没有用。我的猜测是setStationPopup.showAtLocation(findViewById(R.id.drawer_layout), Gravity.CENTER, 0, 0); Gravity.CENTER的行为不符合预期。

View stationPopView = getLayoutInflater().inflate(R.layout.popupsetstartpos, null);
setStationPopup = new PopupWindow(stationPopView);
setStationPopup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setStationPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setStationPopup.showAtLocation(findViewById(R.id.drawer_layout), Gravity.CENTER, 0, 0);
setStationPopup.update();

弹出窗口所在的layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/livebackground"
    android:id="@+id/drawer_layout"
    tools:openDrawer="start">
    <!--tools:context="com.example.erikbylow.skeet25private.LiveSession"-->


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:clipToPadding="false"
        android:orientation="vertical"
        android:layout_marginTop="?attr/actionBarSize"
        android:gravity="center_horizontal">

        <!--<Spinner
            android:gravity="center"
            android:id="@+id/add_spinner_disciplines"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="0.1"
            android:textSize="30dp"
            ></Spinner>-->

        <FrameLayout
            android:fitsSystemWindows="true"
            android:clipToPadding="false"
            android:id="@+id/live_session_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.65"></FrameLayout>


        <FrameLayout
            android:id="@+id/live_button_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25">

        </FrameLayout>
    </LinearLayout>

    <!--ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/blue"
        ></ListView-->

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:clipToPadding="false"

        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

弹出窗口的layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:id="@+id/popupwindow">

    <TextView
        android:gravity="center"
        android:layout_width="410dp"
        android:layout_height="100dp"
        android:text="Set your start position"
        android:textSize="24sp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_green_light"
        android:textColor="@android:color/black"/>
    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button1"
            android:layout_width="@dimen/station_button_width"
            android:layout_height="@dimen/station_button_height"
            android:text="1"
            android:clickable="true"
            android:onClick="station1"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="@dimen/station_button_width"
            android:layout_height="@dimen/station_button_height"
            android:text="2"
            android:clickable="true"
            android:onClick="station2"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="@dimen/station_button_width"
            android:layout_height="@dimen/station_button_height"
            android:text="3"
            android:onClick="station3"
            android:clickable="true"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <Button
            android:layout_width="@dimen/station_button_width"
            android:layout_height="@dimen/station_button_height"
            android:text="4"
            android:onClick="station4"
            android:clickable="true"/>
        <Button
            android:layout_width="@dimen/station_button_width"
            android:layout_height="@dimen/station_button_height"
            android:text="5"
            android:onClick="station5"
            android:clickable="true"/>
    </LinearLayout>
</LinearLayout>

问题是弹出窗口不是某些手机的中心。例如,在我堂兄的Xperia Z5Android version 7.0上,它看起来像: enter image description here

在我的所有具有不同屏幕尺寸和分辨率的虚拟设备上,以及我的测试手机,Xperia Z5Nexus 5看起来像

enter image description here

我真的不明白这种行为。GRAVITY.Center适用于所有设备。此外,drawer_layout的大小设置为match_parent,因此我希望它们能够填满整个屏幕。

1 个答案:

答案 0 :(得分:0)

尝试删除:

android:fitsSystemWindows="true"

或使用android:fitsSystemWindows="false"

在您layout.xml的{​​{1}}以及其他版面中。

<强> fitsSystemWindows

  

系统窗口是系统绘制的屏幕部分   非交互式(在状态栏的情况下)或交互式   (在导航栏的情况下)内容。大部分时间,您的应用程序   不需要在状态栏或导航栏下绘制,但如果   你这样做:你需要确保交互式元素(如按钮)   并没有隐藏在他们之下。这就是默认行为   DrawerLayout属性为您提供:它设置了   填充视图以确保内容不会覆盖系统   窗户。