Android popupwindow簡單使用方法介紹
更新時間:2016年12月29日 15:01:05 作者:JH_Manny
這篇文章主要為大家詳細介紹了Android popupwindow簡單使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
先看下效果
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對象 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ù),點擊外邊可消失 kindsPopupWindow.setBackgroundDrawable(new BitmapDrawable()); //設(shè)置點擊窗口外邊窗口消失 kindsPopupWindow.setOutsideTouchable(true); // 設(shè)置此參數(shù)獲得焦點,否則無法點擊 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
在Android中動態(tài)添加Panel框架的實現(xiàn)代碼
項目經(jīng)常會有這種需求,就是想動態(tài)的在某個界面上添加一個Panel。比如,有一個按鈕,點擊后會顯示下拉菜單式的界面。這種需求,就屬于動態(tài)添加一個Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開發(fā),所以就有了以下內(nèi)容2013-05-05android全屏去掉title欄的多種實現(xiàn)方法
android全屏去掉title欄包括以下幾個部分:實現(xiàn)應(yīng)用中的所有activity都全屏/實現(xiàn)單個activity全屏/實現(xiàn)單個activity去掉title欄/自定義標(biāo)題內(nèi)容/自定義標(biāo)題布局等等感興趣的可參考下啊2013-02-02Android Studio生成 Flutter 模板代碼技巧詳解
這篇文章主要為大家介紹了Android Studio生成 Flutter 模板代碼技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Android Studio中導(dǎo)入JNI生成的.so庫的實現(xiàn)方法
這篇文章主要介紹了Android Studio中導(dǎo)入JNI生成的.so庫的實現(xiàn)方法的相關(guān)資料,這里不僅提供實現(xiàn)方案并提供了實現(xiàn)的方法,需要的朋友可以參考下2017-07-07Android中fragment嵌套fragment問題解決方法
這篇文章主要介紹了Android中fragment嵌套fragment問題解決方法,本文給出兩個解決方法,需要的朋友可以參考下2015-06-06