【发布时间】:2016-04-01 16:45:06
【问题描述】:
我创建了这个弹出菜单,但缺少背景阴影。我怎样才能添加一些?如果阴影只在左边和底部就好了。
这是一张图:可以看到弹窗的颜色和工具栏下方的activity的背景是齐头并进的。
这是我的代码:
活动sn-p
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:background="@color/white">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
编辑:
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));
}
【问题讨论】:
-
您可以将RelativeLayout的背景更改为带有阴影的照片或形状。
-
@tinysunlight 你能发一个例子吗?
-
你可以使用popupwindow而不是popupmenu,popupwindow比popupmenu有更多的功能
-
使用这个布局github.com/ShogoMizumoto/ZDepthShadow作为父布局