当单击按钮时,我想显示来自我的PlaceHodler类的扩展Fragment的d出窗口.为了进行测试,我编写了此代码,该代码确实有效,但我认为这很荒谬(使用button对象作为父视图,依此类推……我找不到其他使它起作用的方法…).请查看此代码,并为我提供改进建议.请不要判断我,因为我是编程的初学者.
我的代码:
public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.fragment_main, container, false); final button button1 = (button)rootVIEw.findVIEwByID(R.ID.button1); button1.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { Log.i("ilog", "onClick() works"); PopupWindow pw = new PopupWindow(getActivity()); TextVIEw tv = new TextVIEw(getActivity()); LayoutParams linearparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); tv.setLayoutParams(linearparams1); tv.setText("Testing"); pw.setContentVIEw(tv); pw.setWIDth(400); pw.setHeight(180); pw.showAtLocation(button1, Gravity.CENTER_HORIZONTAL, 25, 25); pw.update(); } }); return rootVIEw; }}
解决方法:
popwindow在片段和活动中几乎相同,除了它们使您获得contrext的方式外,在活动this中,片段getActivity()
这是创建popWindow的代码
VIEw popupVIEw = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null); final PopupWindow popupWindow = new PopupWindow(popupVIEw, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);// define your vIEw here that found in popup_layout // for example let consIDer you have a button button btn = (button) popupVIEw.findVIEwByID(R.ID.button);// finally show up your popwindowpopupWindow.showAsDropDown(popupVIEw, 0, 0);
参考PopupWindow
总结以上是内存溢出为你收集整理的android-显示来自片段的d出窗口全部内容,希望文章能够帮你解决android-显示来自片段的d出窗口所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)