02.08PopupWindow
Create the PopupWindow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
PopupWindow popup; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); popup= new PopupWindow(inflater.inflate(R.layout.popup_layout,null,false)); popup.setFocusable(true); popup.setOutsideTouchable(true); //If you want it to work correctly, be sure to add a background. popup.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.popup_background)); popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } |
PopupWindow popup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popup= new PopupWindow(inflater.inflate(R.layout.popup_layout,null,false));
popup.setFocusable(true);
popup.setOutsideTouchable(true);
//If you want it to work correctly, be sure to add a background.
popup.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.popup_background));
popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
Making a change to the PopupWindow:
1 2 |
TextView text = ((TextView) popup.getContentView().findViewById(R.id.popupText)); text.setText("PopupWindow Example"); |
TextView text = ((TextView) popup.getContentView().findViewById(R.id.popupText));
text.setText("PopupWindow Example");
Showing the PopupWindow:
1 2 |
if(!popup.isShowing()) popup.showAsDropDown(getActivity().findViewById(R.id.display_below_me), 5, 5); |
if(!popup.isShowing())
popup.showAsDropDown(getActivity().findViewById(R.id.display_below_me), 5, 5);
popup_layout.xml
1 2 3 4 5 6 7 8 9 10 |
< ?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:baselineAligned="false" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <textview android:id="@+id/popupText" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout> |
< ?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<textview android:id="@+id/popupText" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</linearlayout>
