2016-04-01 4 views
3

Ich habe dieses Popup-Menü erstellt, aber der Hintergrund Schatten fehlt. Wie kann ich etwas hinzufügen? Es wäre cool, wenn der Schatten nur links und unten wäre.Android Popup Menü Shadow

Hier ist ein Bild: Sie sehen, dass die Farbe des Popup und der Hintergrund der Aktivität unter der Symbolleiste Hand in Hand gehen.

picture

ist hier mein Code:

acitivity Snippet

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> 

EDIT:

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

Sie können Hintergrund von RelativeLayout zu einem Foto oder einer Form mit Schatten ändern. –

+0

@tinysunlight können Sie ein Beispiel pls veröffentlichen? –

+0

Sie können das Popup-Fenster verwenden, dann Popup-Menü, Popup-Fenster hat mehr Funktion als Popup-Menü –

Antwort

4

hatte ich das gleiche Problem vor ein paar Tagen :) Das ist, wie ich es gelöst. Im Folgenden Link auf eine Website nehmen, wo Sie einen Schatten erzeugen können, was Sie wollen :)

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

Dies ist ein 9-Patch-Bild :) alles, was Sie das erledigt zu tun :)

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

Thats it :) Sie sind fertig :)

+0

getDrawable() erfordert api 21. Mein min ist 16. –

+0

wenn Sie habe noch ein Problem contextCompat.getDrawable (Kontext, R.drawable. ***) Hier ist der Link http://stackoverflow.com/questions/27796246/android-alternative-for-context-getdrawable –

+0

Sie können einen Blick darauf werfen auch dies :) http://StackOverflow.com/Questions/29041027/android-Gettresources-GetDrawable-deprecated-api-22/29041466#29041466 –

1

ich denke, der einfachste Weg, dies nach Ansicht der Erhebung durch Einstellung zu tun ist, die in API 21 und höher zur Verfügung steht. Das ist, was gut für mich funktionierte:

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