2016-04-01 53 views
3

我創建了這個彈出菜單,但背景陰影丟失。我如何添加一些?如果影子只在左邊和底部,這將是很酷的。Android Popup Menu Shadow

下面是一張圖片:您可以​​看到彈出窗口的顏色和工具欄下方的活動背景是齊頭並進的。

picture

這裏是我的代碼:

胡亞蓉片斷

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

您可以將RelativeLayout的背景更改爲帶有陰影的照片或形狀。 –

+0

@tinysunlight你可以發表一個例子嗎? –

+0

你可以使用popupwindow而不是popup菜單,popupwindow有更多的功能,然後popupmenu –

回答

4

我前幾天有同樣的問題:)這是我如何解決它。下面的鏈接將帶你到一個網站,你可以產生任何你想要的:)陰影

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

完蛋了:)你完成了:)

+0

getDrawable()需要API 21.我的分鐘是16. –

+0

如果你仍然有問題嘗試這種ContextCompat.getDrawable(上下文,R.drawable。***)這裏是鏈接http://stackoverflow.com/questions/27796246/android-alternative-for-context-getdrawable –

+0

你可以看看在這個以及:) http://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22/29041466#29041466 –

1

我認爲最簡單的方法做,這是通過設置視圖的高度,這是API 21及更高版本。這對我來說很好:

if (Build.VERSION.SDK_INT >= 21) { 
    popupWindow.setElevation(10); 
}