微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想根据android中的标题大小在运行时设置弹出窗口宽度

我尝试了很多东西,但没有得到任何解决方案,我在运行时设置文本,我需要根据文本大小弹出的宽度
LinearLayout viewGroup =(LinearLayout)((Activity)context)
.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context

        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = layoutInflater.inflate(R.layout.popup_layout,viewGroup);
        title_tv = (TextView) layout.findViewById(R.id.popup_txt);

        title_tv.setText(title_P);


         PopupWindow popup = new PopupWindow(context);
final float SCALE = layout.getResources().getdisplayMetrics().density;
int mode = MeasureSpec.getMode(View.MeasureSpec.UNSPECIFIED);
        int measuredWidth = MeasureSpec.getSize(View.MeasureSpec.UNSPECIFIED);
 popup.setWidth(measuredWidth);
         popup.setHeight(60);


        popup.setContentView(layout);
        //popup.setwindowLayoutMode();

        popup.setFocusable(true);

解决方法

使用:
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

加上,如果适用:

popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

但是,请确保您没有在popup_layout的内容中使用fill / match_parent,因为这没有意义(例如,弹出窗口设置为包装其内容,而内容表示让我和我父母一样大).发布popup_layout XML以获得更准确的答案.

原文地址:https://www.jb51.cc/android/316699.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐