2017-04-22 41 views
0

在我的应用程序中,我有不同的弹出窗口。在我的手机和所有虚拟设备上,它们都完美地集中在屏幕上。不幸的是,所有手机都不是这种情况。android中的居中弹出窗口不适用于所有手机

在我的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,所以我期望它们填满屏幕。

回答

0

尝试删除:

android:fitsSystemWindows="true" 

或使用您的layout.xmlDrawerLayout,也从其他布局android:fitsSystemWindows="false"

fitsSystemWindows

系统窗口是其中系统绘制 任一非交互(在状态栏的情况下)或交互式 屏幕的部分(在导航的情形中酒吧)content.Most时间,你的应用程序 将不需要在状态栏或导航栏下绘制,但如果你做:你需要确保交互式元素(如按钮) 不隐藏在下面他们。这就是 android:fitsSystemWindows=“true”属性的默认行为给你的:它设置了视图的 填充以确保内容不会覆盖系统的 窗口。

+0

Thx,我会试试看。 –

+0

不幸的是,它似乎并没有工作。仍然放在我表弟的Z5 –

+0

上的屏幕上方,您已在布局中添加了'fitsworkWindows'四次...您需要将它们全部删除.. – rafsanahmad007