Android:创建的弹出窗口未在设备上清晰显示

时间:2016-10-10 11:08:00

标签: java android popup popupwindow

我使用xml布局文件创建了一个弹出窗口。我用它来显示单击按钮时的说明。

这是我的弹出窗口代码,

popup_window.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pop_txtView1"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="Theme"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pop_txtView2"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="Full"
        android:textSize="20sp"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pop_txtView3"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/full_detail"
        android:textSize="16sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pop_txtView4"
        android:layout_marginTop="48dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="Lite"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pop_txtView5"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/lite_detail"
        android:textSize="16sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="40dp"
        android:layout_gravity="right">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pop_txtView6"
            android:textSize="20sp"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pop_txtView7"
            android:text="Cancel"
            android:layout_marginLeft="40dp"
            android:clickable="true"
            android:textSize="20sp"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pop_txtView8"
            android:text="OK"
            android:layout_marginLeft="10dp"
            android:clickable="true"
            android:textSize="20sp"
            android:layout_weight="1"/>

    </LinearLayout>


</LinearLayout>

这就是我打电话到弹出窗口的方式,

private PopupWindow popupWindow;

@InjectView(R.id.ib_question)
ImageButton qestionImageButton;

@OnClick(R.id.ib_question)
public void showPopup(){

      try{
          LayoutInflater inflater = (LayoutInflater) ProfileUserActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View layout = inflater.inflate(R.layout.popup_window,
                    (ViewGroup) findViewById(R.id.popup_layout));
          popupWindow = new PopupWindow(layout, 300, 370, true);
          popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);

      }catch (Exception ex){
          ex.printStackTrace();
      }

}

单击此图像按钮时,我无法将弹出窗口和活动分开。

下面的屏幕截图让您清楚了解我的问题。

enter image description here

对此有任何想法吗?

1 个答案:

答案 0 :(得分:1)

你必须像这样设置

LayoutInflater inflater = (LayoutInflater) ProfileUserActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View layout = inflater.inflate(R.layout.popup_window,null);
  popupWindow = new PopupWindow (layout ,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
  popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
相关问题