PopupWindow showAsDropDown() 不能水平居中

问题描述

我要实现ui结果:

enter image description here

但我明白了:

enter image description here

和资源代码是:

  • 主要活动
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,Gravity.CENTER_HORIZONTAL);
           }
       },200);

   }
}
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);

    }


}

用户输入一些字母时,我们的应用程序需要给出建议,例如输入 A,我们需要给 Alice。而且进展不顺利,所以我需要帮助,我想将 PopupWindow 居中水平。但是 PupupWindow 被留下了。我的设备是在 Android 9. 我该如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...