创建一个popwindow 并动态设置pop的高度 限定pop高度

创建一个popwindow 并动态设置pop的高度 限定pop高度

这里举个例子,pop里面放的是一个listview

直接上代码

SelectMedicalCasePopwindow。java

public class SelectMedicalCasePopwindow extends PopupWindow implements OnClickListener, AdapterView.OnItemClickListener {

   private TextView tv;
   private View mMenuView;
   private Context context;
   private Handler callback;

   private ListView mMyListView;
   private String[] items;
   private Button mButton;
   private ChatHistoryAdapter mAdapter;

   public SelectMedicalCasePopwindow(Activity con, List<String> itemList) {
      this(con, itemList.toArray(new String[itemList.size()]));
   }

   public SelectMedicalCasePopwindow(Activity con, String[] items) {
      super(con);
      this.context = con;
      this.items = items;
      int scrheight = DensityUtil.getScreenHeight(context)  / 2;
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      mMenuView = inflater.inflate(R.layout.select_medical_case_pop, null);
      mAdapter = new ChatHistoryAdapter();
      mMyListView = (ListView) mMenuView.findViewById(R.id.bithday_layout);

      View listItem = mAdapter.getView(0, null, mMyListView);
      listItem.measure(0, 0);
      int relheight = listItem.getMeasuredHeight();
      UtilsLog.d(scrheight);
      UtilsLog.d(relheight);


      this.setContentView(mMenuView);
      this.setWidth(LayoutParams.MATCH_PARENT);
      if(relheight * items.length > scrheight){
         LayoutParams layoutParams = mMyListView.getLayoutParams();
         layoutParams.width = LayoutParams.MATCH_PARENT;
         layoutParams.height = scrheight;
         mMyListView.setLayoutParams(layoutParams);
         //this.setHeight(scrheight);
      } else {
      }
      this.setHeight(LayoutParams.WRAP_CONTENT);

      // 设置SelectPicPopupWindow弹出窗体可点击
      this.setFocusable(true);
      // 设置SelectPicPopupWindow弹出窗体动画效果
      this.setAnimationStyle(R.style.AnimBottom);
      // 实例化一个ColorDrawable颜色为半透明
      ColorDrawable dw = new ColorDrawable(0xb0000000);
      // 设置SelectPicPopupWindow弹出窗体的背景
      this.setBackgroundDrawable(dw);
      // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
      mMenuView.setOnTouchListener(new View.OnTouchListener() {

         public boolean onTouch(View v, MotionEvent event) {

            int height = mMenuView.findViewById(R.id.bithday_layout_lin).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
               if (y < height) {
                  dismiss();
               }
            }
            return true;
         }
      });

      mButton = (Button) mMenuView.findViewById(R.id.btn_cancel);
      mButton.setOnClickListener(this);
      mMyListView.setAdapter(mAdapter);
      mMyListView.setOnItemClickListener(this);
      this.update();
   }

   @Override
   public void showAtLocation(View parent, int gravity, int x, int y) {
      super.showAtLocation(parent, gravity, x, y);
      this.tv = (TextView) parent;
   }

   public void showAtLocationNoSetText(View parent, int gravity, int x, int y, Handler callback) {
      super.showAtLocation(parent, gravity, x, y);
      this.callback = callback;
   }


   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.btn_cancel:
            this.dismiss();
            if (callback != null) {
               callback.sendEmptyMessage(0);
            }
            break;

      }

   }

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      if (callback != null) {
         Message msg = callback.obtainMessage(1, position, 0);
         callback.sendMessage(msg);
      }
      dismiss();
   }

   class ChatHistoryAdapter extends BaseAdapter {

      @Override
      public int getCount() {
         return items.length;
      }

      @Override
      public Object getItem(int position) {
         return null;
      }

      @Override
      public long getItemId(int position) {
         return position;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
         ChatHistoryHolder holder;
         if (convertView == null) {
            holder = new ChatHistoryHolder();
            convertView = View.inflate(context, R.layout.view_chat_select_case_item, null);
            holder.clnic_doctor_item_time_tv = (TextView) convertView.findViewById(R.id.btn_myquestion_first);
            convertView.setTag(holder);
         } else {
            holder = (ChatHistoryHolder) convertView.getTag();
         }
         holder.clnic_doctor_item_time_tv.setText(items[position]);
         return convertView;
      }

      class ChatHistoryHolder {
         TextView clnic_doctor_item_time_tv;

      }

   }
}


select_medical_case_pop.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bithday_layout_lin"
        android:background="@color/white">


        <TextView
            android:id="@+id/choose_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="13dip"
            android:text="选择病例"
            android:gravity="center"
            android:layout_centerInParent="true"
            android:textColor="@color/text_color_959595"
            android:textSize="@dimen/text_size_22px" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dip"
            android:layout_below="@+id/choose_txt"
            android:background="@color/text_color_cccccc" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/bithday_layout_lin"
        android:layout_width="match_parent"
        android:layout_above="@+id/bithday_layout1"
        android:background="@color/white"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/bithday_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:divider="@color/text_color_cccccc"
            android:dividerHeight="0.1dp" />
    </LinearLayout>

    <View
        android:id="@+id/bithday_layout1"
        android:layout_width="match_parent"
        android:layout_height="10dip"
        android:layout_above="@+id/layout_cancel"
        android:background="#f2f5f7" />

    <LinearLayout
        android:id="@+id/layout_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/white"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="match_parent"
            android:layout_height="@dimen/height50"
            android:background="@drawable/freeclinics_btn_click_bg"
            android:gravity="center"
            android:text="取 消"
            android:textColor="@color/content"
            android:textSize="@dimen/text_size_20px" />
    </LinearLayout>

</RelativeLayout>


view_chat_select_case_item.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="55dip"
    android:background="@drawable/freeclinics_btn_click_bg">


    <TextView
        android:id="@+id/btn_myquestion_first"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/freeclinics_btn_click_bg"
        android:gravity="center"
        android:padding="13dip"
        android:text="男"
        android:textColor="@color/content"
        android:textSize="@dimen/text_size_22px" />
</LinearLayout>







  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android西红柿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值