美文网首页
控件问题

控件问题

作者: 已经是咸鱼的小涛orz | 来源:发表于2017-09-12 17:12 被阅读0次

    Button自边距

    部分主题下(如AppCompatActivity),Button会自带边距,并且padding、margin设为0也没用;

    android:minWidth="0dp"
    

    PopupWindow关闭

    点击外部取消和点击返回键取消

    final PopupWindow popWindow = new PopupWindow(
            view,
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 
            true  //  点击外部取消,还不行添加下面注释代码
    );
    //      popWindow.setTouchable(true);
    //      popWindow.setOutsideTouchable(true);
    //      popWindow.setBackgroundDrawable(new ColorDrawable());
    //  点击返回键取消
    view.setOnKeyListener(new View.OnKeyListener(){
        @Override
        public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
            if (arg1 == KeyEvent.KEYCODE_BACK){
                popWindow.dismiss();
            }
            return false;
        }
    });
    

    ImageView图片适配

            LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) ivLogo.getLayoutParams();
            Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg_about);
            linearParams.width = DisplayUtil.getScreenWidth(this);
            linearParams.height = (int) (DisplayUtil.getScreenWidth(this) / (float) drawable.getIntrinsicWidth() * drawable.getIntrinsicHeight());
            ivLogo.setLayoutParams(linearParams);
    

    点击编辑框外部取消软键盘

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            View view = getCurrentFocus();
            if (view != null && ev.getAction() == MotionEvent.ACTION_DOWN && view instanceof EditText) {
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                int x = location[0], y = location[1];
                if(ev.getX() < x || ev.getX() > (x + view.getWidth()) || ev.getY() < y || ev.getY() > (y + view.getHeight())){
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    }
                }
            }
            return super.dispatchTouchEvent(ev);
        }
    

    相关文章

      网友评论

          本文标题:控件问题

          本文链接:https://www.haomeiwen.com/subject/qhacsxtx.html