【问题标题】:ListPopupWindow marign bottom not workingListPopupWindow 边缘底部不起作用
【发布时间】:2017-02-20 06:14:07
【问题描述】:

我试图在单击列表视图项时显示 ListPopupWindow。 ListPopupWindow 中的项目是可滚动的。但我不能给它底部边距,它触及屏幕的底部。

private ListPopupWindow getListMenu(Context context, View anchor, String[] options) {
    Context wrapper = new ContextThemeWrapper(context, R.style.ProfilePicPopUp);
    ListPopupWindow popupWindow = new ListPopupWindow(wrapper);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
            R.layout.listpopup_item, options);
    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    popupWindow.setAdapter(adapter);
    popupWindow.setAnchorView(anchor);
    popupWindow.getAnimationStyle();
    popupWindow.setModal(true);
    popupWindow.setHorizontalOffset( (anchor.getWidth()*3)/4);
    popupWindow.setVerticalOffset(-anchor.getHeight()/4);

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    popupWindow.setWidth((width * 3) / 4); // note: don't use pixels, use a dimen resource


    return popupWindow;
}

listpopup_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    fontPath="Lato-Bold.ttf"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/profilepic_option_click_selector"
    android:ellipsize="marquee"
    android:gravity="center_vertical"
    android:paddingBottom="7dp"
    android:paddingEnd="15dip"
    android:paddingStart="14dip"
    android:paddingTop="7dp"
    android:text="sample"
    android:textColor="@color/white" />

【问题讨论】:

  • 你能发布你的xml布局吗?

标签: android popup margins


【解决方案1】:

我在 ListPopupWindow 中浏览代码,我包含一个 listview,所以你可以试试这个代码来设置 listview 的底部边距。

   ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) listPopupWindow.getListView()
            .getLayoutParams();

    mlp.setMargins(0, 0, 0, yourMarginBottom);

如果你想要一个灵活的ListView,你可以创建一个PopupWindow的自定义视图

【讨论】:

    【解决方案2】:

    试试这个。查看填充元素。

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:padding="10dip"        
       android:background="@color/colorbg">    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2015-02-19
      • 2023-03-04
      • 1970-01-01
      • 2014-10-10
      相关资源
      最近更新 更多