android 菜单设置阴影,Android弹出菜单阴影

我创建了这个弹出菜单,但缺少背景阴影.我该如何添加一些?如果阴影只在左侧和底部,那将会很酷.

这是一张图片:您可以​​看到弹出窗口的颜色和工具栏下方活动的背景齐头并进.

9fPgv.png

这是我的代码:

活动片段

public void showPopup(final MenuItem menuItem) {

View view = findViewById(R.id.action_alarm);

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

View popupView = layoutInflater.inflate(R.layout.popup, null);

final ListView listView = (ListView) popupView.findViewById(R.id.listView);

String[] functions = {getString(R.string.benachrichtigung), getString(R.string.benachrichtigungUm)};

final ListAdapter adapter = new CustomPopupAdapter(this, functions, listView);

listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

TextView tv = (TextView) listView.getChildAt(1).findViewById(R.id.tvTime);

showTimePickerDialog(tv);

}

});

PopupWindow popupWindow = new PopupWindow(

popupView,

ViewGroup.LayoutParams.WRAP_CONTENT,

ViewGroup.LayoutParams.WRAP_CONTENT);

popupWindow.setBackgroundDrawable(new BitmapDrawable());

popupWindow.setOutsideTouchable(true);

popupWindow.showAsDropDown(view);

}

popup.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="5dp"

android:background="@color/white">

android:id="@+id/listView"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

编辑:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_192256));

} else {

popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_192256));

}

解决方法:

前几天我遇到了同样的问题:)这就是我解决它的方法.以下链接将带您到一个网站,您可以随心所欲地生成阴影:)

这是一个9补丁图片:)一旦完成,你所要做的就是:)

Display display = (yourActivity.getWindowManager().getDefaultDisplay();

Point size = new Point();

display.getSize(size);

int width = size.x;

int height = size.y;

Resources resources = yourActivity.getResources();

int navigationBarHeight = 0;

int statusbarHeight = 0;

int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");

if (resourceId > 0) {

navigationBarHeight = resources.getDimensionPixelSize(resourceId);

}

resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");

if (resourceId > 0) {

statusbarHeight = resources.getDimensionPixelSize(resourceId);

}

popupWindow = new PopupWindow(yourActivity);

popupWindow.setContentView(yourlayout);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

popupWindow.setBackgroundDrawable(resources.getDrawable(R.drawable.shadow, yourActivity.getTheme()));

} else {

popupWindow.setBackgroundDrawable(resources.getDrawable(R.drawable.shadow));

}

popupWindow.setWidth(width - 20);//20 is padding i have added 10 from right 10 from left

popupWindow.setHeight(height - (navigationBarHeight +40));

popupWindow.setOutsideTouchable(true);

popupWindow.setFocusable(true);

popupWindow.showAtLocation(youractivityView, Gravity.CENTER, 0, statusbarHeight);

多数民众赞成:)你完成了:)

标签:android,menu,popup,shadow

来源: https://codeday.me/bug/20190829/1759174.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值