Android 如何设置列表弹出窗口的圆形背景?
我有一个菜单,它有一个锐利的矩形背景。我已经尝试了很多,但我无法将其更改为圆形背景。 弹出菜单.xmlAndroid 如何设置列表弹出窗口的圆形背景?,android,android-styles,material-components-android,popupmenu,android-popupwindow,Android,Android Styles,Material Components Android,Popupmenu,Android Popupwindow,我有一个菜单,它有一个锐利的矩形背景。我已经尝试了很多,但我无法将其更改为圆形背景。 弹出菜单.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android
<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();
}
public static void showAlbumPopupOptions(最终活动活动、ImageView列表更多、,
最终歌曲,弦乐歌曲ID,int playingState){
最终布尔值isLogged=AppController.getBooleanPreference(Constants.LOGGED_IN,false);
字符串[]列表项;
listItems=activity.getResources().getStringArray(R.array.popup\u菜单\u添加\u相册);
ArrayAdapter mPopupAdapter=新的ArrayAdapter(活动,R.layout.popup_菜单,R
.id.详细信息,列表项目);
最终ListPopupWindow albumPopup=新ListPopupWindow(活动);
albumPopup.setContentWidth(Utils.measureContentWidth(mPopupAdapter,activity));
albumPopup.setAdapter(mPopupAdapter);
albumPopup.setHeight(ListPopupWindow.WRAP_内容);
albumPopup.setAnchorView(列表更多);
albumPopup.setModal(true);
albumPopup.setDropDownGravity(Gravity.END);
最终加载进度对话框加载进度对话框=新建加载进度对话框(活动,R
风格
.DialogThemeProgress,false);
albumPopup.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
如果(位置==0){
如果(已记录){
DialogPlayList DialogPlayList=新建DialogPlayList(活动,
activity,String.valueOf(song.getId()),loadingProgressDialog);
dialogPlayList.show();
loadingProgressDialog.setDimDialog(false);
加载ProgressDialog.showProgress();
}否则{
重定向登录(活动);
}
albumposp.discouse();
}否则如果(位置==2){
showAudioShare(歌曲、活动);
albumposp.discouse();
}否则如果(位置==1){
AppController.addToQueueSongs(活动、歌曲、歌曲ID、,
播放状态);
albumposp.discouse();
}
}
});
albumPopup.show();
}
当用户单击列表中的“更多”选项时,将调用上述给定的方法。我正在从这个方法向列表中添加数据。因此,我得到了这个
但实际上我想要的是下面给出的图片
我尝试过将背景设置为圆角,但没有用。我真的不知道如何实现。请给我建议一些解决办法。提前谢谢。我找到了答案。在设置适配器之前,使用ListPopupWindow添加此代码
Drawable background = ContextCompat.getDrawable(activity, R.drawable
.res_black_menuroundfilled_corner);
albumPopup.setBackgroundDrawable(background);
删除android:background=“#1F2026”
中的弹出菜单.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>
下面给出的代码是用于可绘制文件的res_black_menuroundfilled_角落
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#1F2026"/>
<corners android:radius="15dp"/>
</shape>
使用材质组件主题您有一个默认圆角背景,圆角半径为
4dp
您可以使用应用程序主题中的listPopupWindowStyle
属性对其进行自定义:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="listPopupWindowStyle">@style/myListPopupWindow</item>
</style>
@样式/myListPopupWindow
与:
@可拉拔/弹出菜单背景
其中可绘制的背景类似于:
<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>
单击点时,您还可以创建一个自定义对话框,并按照您的预期制作一个屏幕@Chandramohan。
ListPopupWindow listPopupWindow = new ListPopupWindow(this);
listPopupWindow.setAdapter(popUpAdapter);
listPopupWindow.setAnchorView(itemView);
listPopupWindow.setDropDownGravity(Gravity.NO_GRAVITY);
listPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.round_corner));
listPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.show();