如何解决PopupWindow showAsDropDown() 不能水平居中
我要实现ui结果:
但我明白了:
和资源代码是:
- 主要活动
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 举报,一经查实,本站将立刻删除。