Android Popupwindow彈出窗口的簡單使用方法
本文實(shí)例為大家分享了Android Popupwindow彈出窗口的具體代碼,供大家參考,具體內(nèi)容如下
代碼很簡單,沒有和別的控件連用。布局自己隨意定義,我的這個(gè)是最基礎(chǔ)的,就直接上代碼啦!
在MainActivity里
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button button = (Button) findViewById(R.id.but);
button.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View view) {
showPopupWindow(view);
}
});
}
private void showPopupWindow(View view) {
// 一個(gè)自定義的布局,作為顯示的內(nèi)容
View contentView =LayoutInflater.from(mContext).inflate(
R.layout.popupwindow, null);
// 設(shè)置按鈕的點(diǎn)擊事件
Button button = (Button) contentView.findViewById(R.id.button );
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "button is pressed",
Toast.LENGTH_SHORT).show();
}
});
final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, true);
// 如果不設(shè)置PopupWindow的背景,無論是點(diǎn)擊外部區(qū)域還是Back鍵都無法dismiss彈框
// 我覺得這里是API的一個(gè)bug
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
// 設(shè)置好參數(shù)之后再show
popupWindow.showAsDropDown(view);
}
}
在主布局里
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
在popupwindow布局里
<?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="#ffff00">
<TextView
android:id="@+id/ttt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="彈出窗口"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)之PopupWindow實(shí)現(xiàn)彈窗效果
- Android彈窗ListPopupWindow的簡單應(yīng)用詳解
- Android使用 PopupWindow 實(shí)現(xiàn)底部彈窗功能
- Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果
- Android開發(fā)實(shí)現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法
- Android編程實(shí)現(xiàn)的自定義彈窗(PopupWindow)功能示例
- Android自定義彈出窗口PopupWindow使用技巧
- Android控件PopupWindow模仿ios底部彈窗
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
- Android中PopupWindow彈出式窗口使用方法詳解
相關(guān)文章
Android Listview 滑動(dòng)過程中提示圖片重復(fù)錯(cuò)亂的原因及解決方法
android中l(wèi)istview是比較常見的組件,通過本文主要給大家分析Android中Listview滾動(dòng)過程造成的圖片顯示重復(fù)、錯(cuò)亂、閃爍的原因及解決方法,順便跟進(jìn)Listview的緩存機(jī)制,感興趣的朋友一起看下吧2016-08-08
Android序列化之Parcelable和Serializable的使用詳解
本篇文章主要介紹了Android序列化之Parcelable和Serializable的使用詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android開發(fā)經(jīng)驗(yàn)談:并發(fā)編程(線程與線程池)(推薦)
這篇文章主要介紹了Android開發(fā)經(jīng)驗(yàn)談:并發(fā)編程(線程與線程池),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android中FloatingActionButton的顯示與隱藏示例
本篇文章主要介紹了Android中FloatingActionButton的顯示與隱藏示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
Android List刪除重復(fù)數(shù)據(jù)
這篇文章主要介紹了Android List刪除重復(fù)數(shù)據(jù)的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06
Android Fragment 和 FragmentManager 的代碼分析
這篇文章主要介紹了Android Fragment 和 FragmentManager 的代碼分析,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路
這篇文章主要介紹了Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路,需要的朋友可以參考下2018-04-04

