Android仿泡泡窗实现下拉菜单条实例代码

功能描述:点击下拉按钮,显示出所有的条目,有删除和点击功能,点击后将条目显示。

注意:泡泡窗默认是没有焦点的。要让泡泡窗获取到焦点。假如listview的item中有Button,ImageButton,CheckBox等会强制获取焦点的view 此时,listview的item无法获取焦点,从而无法被点击 解决方法:给item的根布局增加以下属性 Android:descendantFocusability="blocksDescendants"设置之后,Button获取焦点,item中其他控件也可以获取焦点,如果文本的文字过多,会被后面的图标盖住,首先设置单行android:singleLine="true"再设置右边距就盖不住android:padding="40dp"

需要添加下面三项

//让泡泡窗额度条目获取到焦点 
    popupWindow.setFocusable(true); 
    //设置背景图 
    popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
    popupWindow.setOutsideTouchable(true); 

主布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="#22000000" 
  tools:context=".MainActivity" > 
  <RelativeLayout  
    android:layout_marginTop="40dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    > 
  <EditText 
    android:id="@+id/et" 
    android:singleLine="true" 
    android:padding="40dp" 
    android:layout_width="300dp" 
    android:layout_height="40dp" 
    /> 
  <ImageView  
    android:layout_width="wrap_content" 
    android:layout_height="40dp" 
    android:id="@+id/iv_select" 
    android:layout_centerVertical="true" 
    android:background="@drawable/ic_launcher" 
    android:layout_alignRight="@id/et" 
    /> 
  </RelativeLayout> 
</RelativeLayout> 

每一个条目的布局文件item_list.xml

<?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:padding="5dp" 
  android:orientation="horizontal" > 
  <TextView  
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@drawable/ic_launcher" 
    android:gravity="center_vertical" 
    android:drawablePadding="3dp" 
    android:text="123" 
    android:id="@+id/tv_number" 
    android:layout_weight="1" 
    /> 
  <ImageView  
    android:id="@+id/iv_delete" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher" 
    /> 
</LinearLayout> 

逻辑代码MainActivity.java

package com.ldw.select; 
import java.util.ArrayList; 
import android.app.Activity; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.PopupWindow; 
import android.widget.TextView; 
public class MainActivity extends Activity implements OnClickListener{ 
  private ImageView iv_select; 
  private EditText et; 
  private ArrayList<String> list = new ArrayList<String>(); 
  private TextView tv_number; 
  private ImageView iv_delete; 
  private ListView listView; 
  private PopupWindow popupWindow; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    initView(); 
    initListener();  
    initData(); 
  } 
  private void initView() { 
    setContentView(R.layout.activity_main); 
    iv_select = (ImageView) findViewById(R.id.iv_select); 
    et = (EditText) findViewById(R.id.et); 
  } 
  private void initListener() { 
    iv_select.setOnClickListener(this); 
  } 
  //集合中添加数据 
  private void initData() { 
    for(int i = 0; i< 20; i++){ 
      list.add(1340000000 + i + ""); 
    } 
    initListView(); 
  } 
  //初始化列表 
  private void initListView(){ 
    listView = new ListView(this); 
    //设置listView的背景图 
    listView.setBackgroundResource(R.drawable.ic_launcher); 
    //让listView的滚动条不可见 
    listView.setVerticalScrollBarEnabled(false); 
    MyAdapter adapter = new MyAdapter(); 
    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
        et.setText(list.get(position)); 
        //关闭泡泡窗 
        popupWindow.dismiss(); 
      } 
    }); 
  } 
  //泡泡窗实现点击显示列表,泡泡窗默认是不获取焦点的 
  private void showNumberList(){ 
    if(popupWindow == null){ 
    //泡泡窗的填充,设置宽高 
    popupWindow = new PopupWindow(listView, et.getWidth(), 300); 
    } 
    //让泡泡窗额度条目获取到焦点 
    popupWindow.setFocusable(true); 
    //设置背景图 
    popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
    popupWindow.setOutsideTouchable(true); 
    //第一个参数是依附哪一个参数下,x轴,y轴的偏移量,相对于第一个参数的左下角的位置 
    popupWindow.showAsDropDown(et, 0, 0); 
  } 
  @Override 
  public void onClick(View v){ 
    switch (v.getId()){ 
      case R.id.iv_select: 
        //点击弹出列表 
        showNumberList(); 
        break; 
    } 
  } 
  //listView填充 
  class MyAdapter extends BaseAdapter{ 
    @Override 
    public int getCount() { 
      return list.size(); 
    } 
    @Override 
    public Object getItem(int position) { 
      return null; 
    } 
    @Override 
    public long getItemId(int position) { 
      return 0; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
      final View view = View.inflate(MainActivity.this, R.layout.item_list, null); 
      tv_number = (TextView) view.findViewById(R.id.tv_number); 
      iv_delete = (ImageView) view.findViewById(R.id.iv_delete); 
      tv_number.setText(list.get(position)); 
      //删除按键 
      iv_delete.setOnClickListener(new OnClickListener(){ 
        @Override 
        public void onClick(View v) { 
          //删除条目 
          list.remove(position); 
          //更新页面 
          notifyDataSetChanged(); 
          //根据当前条目的个数设置,当前条目的高度 
          int listViewHeight = list.size() * view.getHeight(); 
          if(listViewHeight > 300){ 
            listViewHeight = 300; 
          }else{ 
            listViewHeight = listViewHeight; 
          } 
          //更新泡泡窗的高度 
          popupWindow.update(et.getWidth(), listViewHeight); 
          //删除完了,泡泡窗小消失 
          if(list.size() == 0){ 
            popupWindow.dismiss(); 
            //下拉条消失 
            iv_select.setVisibility(View.GONE); 
          } 
        } 
      }); 
      return view; 
    } 
  } 
} 

以上所述是小编给大家介绍的Android仿泡泡窗实现下拉菜单条实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!