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

时间:2013-01-04 11:36:58

标签: android android-layout popup popupwindow

我尝试了很多东西,但没有得到任何解决方案,我在运行时设置文本,我需要根据文字大小的弹出宽度     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);

1 个答案:

答案 0 :(得分:17)

使用:

popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

加上,如果适用:

popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

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