PopupWindow showAsDropDown() can not center horizontal

0

I want to implement ui result:

enter image description here

but I get:

enter image description here

and resource code is:

  • MainActivity
public class MainActivity extends AppCompatActivity {
   EditText et;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       et = findViewById(R.id.et);
       new Handler().postDelayed(new Runnable() {
           @RequiresApi(api = Build.VERSION_CODES.KITKAT)
           @Override
           public void run() {
               SearchPopupWindow dialog = new SearchPopupWindow(MainActivity.this);
               dialog.showAsDropDown(et,0,0, Gravity.CENTER_HORIZONTAL);
           }
       }, 200);

   }
}
  • SearchPopupWindow
public class SearchPopupWindow extends BasePopupWindow {
    public SearchPopupWindow(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.popup_layout;
    }
}

  • BasePopupWindow


public abstract class BasePopupWindow extends PopupWindow {


    private Context mContext;


    public BasePopupWindow(Context context) {
        this.mContext = context;
        initView();
    }

    public abstract int getLayoutId();

    private void initView() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        View v = LayoutInflater.from(mContext).inflate(getLayoutId(), linearLayout);
        setContentView(v);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
//        setAnimationStyle(R.style.CustomDialogAnimation);

    }


}

Our App neet to give advices when User input some letter,like input A ,we need give Alice.And It's not going well,so I need help,I want to PopupWindow center horizental.But PupupWindow is left.My device is in Android 9.How can I solve this?

android
popupwindow
android-popupwindow
asked on Stack Overflow Dec 21, 2020 by zhangjin • edited Dec 22, 2020 by zhangjin

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0