android - 如何为列表弹出窗口设置圆角背景?

标签 android android-styles material-components-android popupmenu android-popupwindow

我有一个具有清晰矩形背景的菜单。我已经尝试了很多我无法将其更改为圆形背景。
popup_menu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1F2026"
    android:orientation="horizontal"
    >

<TextView
    android:id="@+id/details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:fontFamily="@font/quicksand_regular"
    android:text="@string/add_to_favourite"
    android:textColor="@color/icon_color_dark" />
</LinearLayout>
上面给出的是我的弹出菜单xml。
 public static void showAlbumPopupOptions(final Activity activity, ImageView listMore,
                                         final Song song, String songId, int playingState) {

    final boolean isLogged = AppController.getBooleanPreference(Constants.LOGGED_IN, false);

    String[] listItems;

    listItems = activity.getResources().getStringArray(R.array.popup_menu_add_album);

    ArrayAdapter<String> mPopupAdapter = new ArrayAdapter<>(activity, R.layout.popup_menu, R
            .id.details, listItems);

    final ListPopupWindow albumPopup = new ListPopupWindow(activity);
    albumPopup.setContentWidth(Utils.measureContentWidth(mPopupAdapter, activity));

    albumPopup.setAdapter(mPopupAdapter);
    albumPopup.setHeight(ListPopupWindow.WRAP_CONTENT);
    albumPopup.setAnchorView(listMore);
    albumPopup.setModal(true);
    albumPopup.setDropDownGravity(Gravity.END);
    final LoadingProgressDialog loadingProgressDialog = new LoadingProgressDialog(activity, R
            .style
            .DialogThemeProgress, false);

    albumPopup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position == 0) {
                if (isLogged) {
                    DialogPlayList dialogPlayList = new DialogPlayList(activity,
                            activity, String.valueOf(song.getId()), loadingProgressDialog);
                    dialogPlayList.show();
                    loadingProgressDialog.setDimDialog(false);
                    loadingProgressDialog.showProgress();
                } else {
                    redirectLogin(activity);
                }
                albumPopup.dismiss();
            } else if (position == 2) {
                showAudioShare(song, activity);
                albumPopup.dismiss();
            } else if (position == 1) {
                AppController.addToQueueSongs(activity, song, songId,
                        playingState);
                albumPopup.dismiss();
            }
        }
    });
    albumPopup.show();
}
当用户单击列表中的更多选项时,将调用上述给定方法。我正在通过这种方法将数据添加到列表中。结果我得到了这个。
screenshot of the menu which has rectangle background
但实际上我想要的是 - 下面给出的图像。
expected output
我尝试将背景设置为圆角,但没有用。我真的不知道如何实现。请给我一些解决方案。提前致谢。

最佳答案

Material 组件主题你有一个默认 圆角背景,圆角半径为 4dp .
enter image description here
您可以使用 对其进行自定义。 listPopupWindowStyle 应用主题中的属性:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="listPopupWindowStyle">@style/myListPopupWindow</item>
</style>
和:
  <style name="myListPopupWindow" parent="Widget.MaterialComponents.PopupMenu.ListPopupWindow">
    <item name="android:popupBackground">@drawable/popupMenuBackground</item>
  </style>
可绘制背景类似于:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/colorSurface"/>

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp"/>
    
    <padding
        android:bottom="8dp"
        android:top="8dp"/>

</shape>
enter image description here

关于android - 如何为列表弹出窗口设置圆角背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62623784/

相关文章:

java - 更改蓝牙查询扫描时间

android - CountDownTimer 不会让我调用 onDestroy

java - 如何在android中获取和设置简单的数学函数?

android - 如何设置 Android MaterialCalendars 各种组件的样式

android - 用 KitKat 的灰色色调覆盖 Android 的蓝色色调的最简单方法是什么?

android - TextInputLayout boxBackgroundColor 在 'com.google.android.material:material:1.1.0-alpha02' 之后不起作用

android - Android 中的白天和夜间模式

Android styles.xml windowNoTitle (ActionBarActivity)

android - Material 日期选择器自定义样式

android - Theme.MaterialComponents.DayNight 未找到