android中弹出窗口,如何在Android中创建弹出窗口(PopupWindow)
如何制作一個簡單的Android彈出窗口
這是一個更完整的例子。這是一個補充性答案,涉及一般情況下創建彈出窗口的過程,而不一定是OP問題的具體細節。(OP要求取消按鈕,但這不是必需的,因為用戶可以在屏幕上的任意位置單擊以將其取消。)它看起來像下圖。
設置彈出窗口的布局
向其中添加一個布局文件,res/layout該文件定義彈出窗口的外觀。
popup_window.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#62def8">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="30dp"
android:textSize="22sp"
android:text="This is a popup window."/>
充氣并顯示彈出窗口
這是示例中主要活動的代碼。只要單擊該按鈕,彈出窗口就會膨脹并顯示在活動上方。觸摸屏幕上的任何位置均可關閉彈出窗口。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
}
而已。大功告成
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android中弹出窗口,如何在Android中创建弹出窗口(PopupWindow)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 葡萄酒减肥吗
- 下一篇: 阿里:不再推进云智能集团完全分拆