我使用stackoverflow的示例代码在OnCreate方法中显示PopUp。我的应用程序没有中断,但它没有显示弹出窗口。我把代码放在下面:
private void showExplain() {
LayoutInflater inflater = this.getLayoutInflater();
final View layout = inflater.inflate(R.layout.popupexplicativo, null);
final PopupWindow windows = new PopupWindow(layout , 300,300,true);
windows.setFocusable(false);
windows.setTouchable(true);
windows.setOutsideTouchable(true);
layout.post(new Runnable() {
public void run() {
windows.showAtLocation(layout,Gravity.CENTER, 0, 0);
}
});
}
在onCreate方法中,我调用showExplain();
任何人都可以帮助我吗?非常感谢!
答案 0 :(得分:0)
试试这个,
popupexplicativo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:padding="10px"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Transfering data"
android:textColor="@color/white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Status"
android:textColor="@color/white"/>
<TextView android:id="@+id/server_status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awaiting answer..."
android:paddingLeft="10sp"
android:textColor="@color/white"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal|bottom">
<Button android:id="@+id/end_data_send_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:drawablePadding="3sp"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
在你的java类中,
private void initiatePopupWindow(View v) {
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) ProfileView.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.popupexplicativo,
(ViewGroup) findViewById(R.id.popup_element));
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, 300, 470, true);
// display the popup in the center
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);
Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
cancelButton.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
我就这样做了:
下面的方法放在一个类Utils中(你可以拥有自己的类名,它只是一个实用程序类,因为我有不同的布局,可以在弹出窗口中膨胀)
public static PopupWindow getPopupWindow(Context context, View parentLayout) {
final PopupWindow popUp = new PopupWindow(context);
popUp.setContentView(parentLayout);
popUp.setWidth(getScreenWidth(context));
popUp.setHeight(getScreenHeight(context));
popUp.setFocusable(true);
popUp.setOutsideTouchable(true);
popUp.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
return popUp;
}
public static int getScreenWidth(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return metrics.widthPixels;
}
public static int getScreenHeight(Context context) {
//int statusBarHeight = isFullScreen() ? 0 : getStatusBarHeight();
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return metrics.heightPixels - getStatusBarHeight(context);
}
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
并在您的活动中使用它
final PopupWindow popUp;
popUp= getPopupWindow(mActivity, yourView);
popUp.showAtLocation(anchorView, Gravity.NO_GRAVITY, 0, 0);
popUp.update(x, y, Utils.getScreenWidth(mActivity), Utils.getScreenHeight(mActivity));