Android popupwindow簡(jiǎn)單使用方法介紹
先看下效果
1.首頁
package com.yskj.jh.demopopupwindow; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private Button button; private PopupWindow kindsPopupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getKindPopupWindow(); } }); } private void getKindPopupWindow() { final ArrayList kindsList = new ArrayList(); kindsList.add("全部分類"); kindsList.add("今日上線"); kindsList.add("美食"); kindsList.add("酒店"); kindsList.add("旅游"); LayoutInflater inflater = LayoutInflater.from(this); // 引入窗口配置文件 View view = inflater.inflate(R.layout.pop_local_kind, null); // 創(chuàng)建PopupWindow對(duì)象 kindsPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, false); //設(shè)置popupWindow寬 kindsPopupWindow.setWidth(button.getWidth()); ListView listView = (ListView) view.findViewById(R.id.list); PopAdapter adapter = new PopAdapter(MainActivity.this,kindsList); listView.setAdapter(adapter); // 需要設(shè)置一下此參數(shù),點(diǎn)擊外邊可消失 kindsPopupWindow.setBackgroundDrawable(new BitmapDrawable()); //設(shè)置點(diǎn)擊窗口外邊窗口消失 kindsPopupWindow.setOutsideTouchable(true); // 設(shè)置此參數(shù)獲得焦點(diǎn),否則無法點(diǎn)擊 kindsPopupWindow.setFocusable(true); //設(shè)置popupWindow顯示位置 kindsPopupWindow.showAsDropDown(button); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this,i+"pop",Toast.LENGTH_SHORT).show(); button.setText(kindsList.get(i).toString()); kindsPopupWindow.dismiss(); } }); } //pop適配器 private class PopAdapter extends BaseAdapter { private Context context; private ArrayList<String> list; public PopAdapter(Context context, ArrayList<String> list) { this.context = context; this.list = list; } @Override public int getCount() { if (list==null||list.size()==0){ return 0; } return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View convertView, ViewGroup viewGroup) { ViewHolder holder = null; if(convertView==null){ holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.item_pop_local_kind, null); holder.textView = (TextView) convertView.findViewById(R.id.item_text); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } holder.textView.setText(list.get(i).toString()); return convertView; } private class ViewHolder { TextView textView; } } }
2.首頁布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show PopupWindow" /> </LinearLayout>
3.popupwindow布局,可根據(jù)情況自行布局,這里是demo布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#888888"> </ListView> </LinearLayout>
4.popupwindow條目布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/white"> <TextView android:id="@+id/item_text" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="pop" android:textColor="#f08e1f" android:background="#eeeeee" /> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Popupwindow彈出窗口的簡(jiǎn)單使用方法
- Android PopupWindow使用方法小結(jié)
- Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出
- Android 使用PopupWindow實(shí)現(xiàn)彈出更多的菜單實(shí)例詳解
- Android組件popupwindow使用方法詳解
- Android自定義彈出窗口PopupWindow使用技巧
- Android中PopupWindow使用方法詳解
- android使用PopupWindow實(shí)現(xiàn)頁面點(diǎn)擊頂部彈出下拉菜單
- Android PopupWindow使用實(shí)例
- PopupWindow使用方法詳解
相關(guān)文章
gradle tool升級(jí)到3.0注意事項(xiàng)小結(jié)
這篇文章主要介紹了gradle tool升級(jí)到3.0注意事項(xiàng)及修改相關(guān)文件介紹,需要的朋友可以參考下2018-02-02在Android中動(dòng)態(tài)添加Panel框架的實(shí)現(xiàn)代碼
項(xiàng)目經(jīng)常會(huì)有這種需求,就是想動(dòng)態(tài)的在某個(gè)界面上添加一個(gè)Panel。比如,有一個(gè)按鈕,點(diǎn)擊后會(huì)顯示下拉菜單式的界面。這種需求,就屬于動(dòng)態(tài)添加一個(gè)Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開發(fā),所以就有了以下內(nèi)容2013-05-05android全屏去掉title欄的多種實(shí)現(xiàn)方法
android全屏去掉title欄包括以下幾個(gè)部分:實(shí)現(xiàn)應(yīng)用中的所有activity都全屏/實(shí)現(xiàn)單個(gè)activity全屏/實(shí)現(xiàn)單個(gè)activity去掉title欄/自定義標(biāo)題內(nèi)容/自定義標(biāo)題布局等等感興趣的可參考下啊2013-02-02Android Studio生成 Flutter 模板代碼技巧詳解
這篇文章主要為大家介紹了Android Studio生成 Flutter 模板代碼技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android Studio中導(dǎo)入JNI生成的.so庫的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Studio中導(dǎo)入JNI生成的.so庫的實(shí)現(xiàn)方法的相關(guān)資料,這里不僅提供實(shí)現(xiàn)方案并提供了實(shí)現(xiàn)的方法,需要的朋友可以參考下2017-07-07Android中fragment嵌套fragment問題解決方法
這篇文章主要介紹了Android中fragment嵌套fragment問題解決方法,本文給出兩個(gè)解決方法,需要的朋友可以參考下2015-06-06android編程實(shí)現(xiàn)對(duì)話框的封裝實(shí)例
這篇文章主要介紹了android編程實(shí)現(xiàn)對(duì)話框的封裝,以實(shí)例形式分析了Android針對(duì)對(duì)話框的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Kotlin Flow常見場(chǎng)景下的使用實(shí)例
這篇文章主要為大家介紹了Kotlin Flow常見場(chǎng)景下的使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android實(shí)現(xiàn)拍照、選擇相冊(cè)圖片并裁剪功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拍照、選擇相冊(cè)圖片并裁剪功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12