gpt4 book ai didi

Android 弹出菜单阴影

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:50 30 4
gpt4 key购买 nike

我创建了这个弹出菜单,但是背景阴影不见了。我怎样才能添加一些?如果阴影只在左侧和底部会很酷。

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

picture

这是我的代码:

Activity fragment

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));
}

最佳答案

几天前我遇到了同样的问题:) 我就是这样解决的。下面的链接将带你到一个网站,在那里你可以生成任何你想要的阴影:)

http://inloop.github.io/shadow4android/

这是一个 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 弹出菜单阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36361626/

30 4 0