android - PopupWindow 的宽度/样式问题

标签 android popupwindow

我正在使用 PopupWindow 在单击 ListView 项目时显示一些子类别。 我对 ListView 行(类别)和 PopupWindow(类别)使用相同的布局。

我的问题是我将 PopupWindow 的宽度设置为我的行项目 View 的宽度,但是当它显示时,它有点窄,带有某种边框。

在下面的屏幕截图中,我将 PopupWindow 放在 ListView 的顶部以查看区别

为什么变小了?我怎样才能去除这个边框?

enter image description here

listView.setOnItemClickListener(...)

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {        

            showPopup(HomeActivity.this, layout_base, v, position);
        }
    }
});

showPopup(...)

public void showPopup(Context context, View parent, View view, int position){

    /*
     * Compute popup position and size
     */

    int[] itemPosition = new int[2];
    view.getLocationOnScreen(itemPosition);

    int item_width = view.getWidth();
    int item_height = view.getHeight();

    int nChildren = GlobalVars.menuItemArray[position].children.size();

    int popup_height = item_height * nChildren; 

    /*
     * Build the view
     */

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout popupContainer = new LinearLayout(context);
    popupContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    popupContainer.setOrientation(LinearLayout.VERTICAL);

    LayoutParams params;

    params = new LayoutParams(item_width, item_height);
    popupContainer.setOrientation(LinearLayout.VERTICAL); 

    for(int i=0; i<nChildren; i++){

        RelativeLayout menu_row = (RelativeLayout) inflater.inflate(R.layout.menu_row, null);        

        popupContainer.addView(menu_row, params);
    }

    mPopup.setContentView(popupContainer);
    mPopup.setHeight(popup_height);
    mPopup.setWidth(item_width);

    mPopup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, 0, 0);         

}

最佳答案

我必须使用以下方法删除 PopupWindow 的背景:

mPopup.setBackgroundDrawable(new BitmapDrawable());

关于android - PopupWindow 的宽度/样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9826132/

相关文章:

java - 在 PopupWindow 中打开 PopupWindow

android - Android 中的对话框和弹出窗口

java - Android studio,在定时器调度之间启动任务(JAVA)

android - java.lang.NullPointerException : displaying records from database in an expandable listview 异常

android - 随着搜索栏的进展,以特定进度触发事件

java - 弹出窗口的背景变暗或模糊

Android:如何拖动(移动)PopupWindow?

android - 如何更改 Material Design ActionBar 的文本?

java - SQL 连接在普通 Java 中有效,但在 Android 中无效

android - ViewPager with Fragments inside PopupWindow(或 DialogFragment) - 错误找不到 fragment id 的 View