2012-03-23 29 views
19

ऐप एस्ट्रिड कार्य में, एक बटन है। जब आप बटन दबाते हैं, तो एक ड्रॉप डाउन मेनू आता है।कस्टम स्पिनर/ड्रॉप डाउन मेनू

enter image description here

enter image description here

यह मूल रूप से एक स्पिनर है, लेकिन एक ड्रॉप-डाउन सूची के रूप में।

क्या कोई जानता है कि कुछ ऐसा कैसे करना है? क्या यह एक विजेट है जो मैं नहीं देखता हूं?

+0

यह एक संदर्भ मेनू –

उत्तर

55

(मैं एस्ट्रिड के लिए प्राथमिक एंड्रॉयड डेवलपर्स में से एक हूँ) इस के मूल लेखक मैं साझा करने के लिए कैसे एस्ट्रिड यह होता है खुशी होगी के रूप में। मैं यहां मूल बातें पोस्ट करूंगा, लेकिन आप हमारे जिथब रेपो (https://github.com/todoroo/astrid) पर अधिक जानकारी प्राप्त कर सकते हैं। हनीड्रॉइड के क्विकएक्शन विजिट को हनी के सुझाव के रूप में विस्तारित करना मूलभूत विचार है। उपवर्ग लग रहा है कि:

public class MenuPopover extends QuickActionWidget { 

    protected DisplayMetrics metrics; 
    protected LinearLayout content; 

    public MenuPopover(Context context) { 
     super(context); 
     setContentView(R.layout.my_layout); 

     content = (LinearLayout) getContentView().findViewById(R.id.content); 
     metrics = context.getResources().getDisplayMetrics(); 

     setFocusable(true); 
     setTouchable(true); 
    } 

    @Override 
    protected void populateQuickActions(List<QuickAction> quickActions) { 
     // Do nothing 
    } 

    @Override 
    protected void onMeasureAndLayout(Rect anchorRect, View contentView) { 
     contentView.setLayoutParams(new  FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT)); 
     contentView.measure(MeasureSpec.makeMeasureSpec(getScreenWidth(),  MeasureSpec.EXACTLY), 
       ViewGroup.LayoutParams.WRAP_CONTENT); 

     int rootHeight = contentView.getMeasuredHeight(); 

     int offsetY = getArrowOffsetY(); 
     int dyTop = anchorRect.top; 
     int dyBottom = getScreenHeight() - anchorRect.bottom; 

     boolean onTop = (dyTop > dyBottom); 
     int popupY = (onTop) ? anchorRect.top - rootHeight + offsetY : anchorRect.bottom - offsetY; 

     setWidgetSpecs(popupY, onTop); 
    } 
} 

लेआउट फ़ाइल my_layout.xml बहुत सरल है:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="10dip"> 

     <LinearLayout 
       android:id="@+id/content" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/gdi_arrow_up" 
       android:orientation="vertical"/> 

     <ImageView 
      android:id="@+id/gdi_arrow_up" 
      android:layout_width="27dip" 
      android:layout_height="27dip" 
      android:layout_marginLeft="-10dip" 
      android:scaleType="fitCenter" 
      android:layout_marginBottom="-8dip" 
      android:src="?attr/asListArrowUp" /> 

     <ImageView 
      android:id="@+id/gdi_arrow_down" 
      android:layout_width="27dip" 
      android:layout_height="27dip" 
      android:scaleType="fitCenter" 
      android:layout_marginBottom="-8dip" 
      android:layout_below="@android:id/list"/> 

     </RelativeLayout> 
</FrameLayout> 

उसके बाद, आप सिर्फ दृश्य जोड़ने के लिए पॉपओवर वर्ग के लिए एक सरल सहायक विधि जोड़ सकते हैं (यानी पंक्तियों , वैकल्पिक श्रोताओं) पॉपओवर के मुख्य शरीर के साथ:

public void addViewToContent(View v, OnClickListener listener) { 
    content.addView(v); 
    if (listener != null) { 
     v.setOnClickListener(listener); 
    } 
} 

पॉपअप का एक उदाहरण बनाने के बाद, आप इसे

फोन करके दिखा सकते हैं
menuPopover.show(anchorView); 

यह कुछ हद तक सरलीकृत संस्करण है - अभ्यास में, हम कुछ अतिरिक्त जानकारी, श्रोताओं इत्यादि को उन दृश्यों से जोड़ते हैं ताकि उन्हें वास्तव में क्लिक करने पर चीजें करने में मदद मिल सके। यदि आप चाहते हैं, तो आप https://github.com/todoroo/astrid पर पूरा कोड देख सकते हैं - कक्षा com.todoroo.astrid.ui.MainMenuPopover है।

एस्ट्रिड का उपयोग करने के लिए धन्यवाद!

+0

वाह इस महान है! तुम बहुत बढ़िया हो, धन्यवाद! (पीएस लव एस्ट्रिड!) – Cole

+14

इसे ओपन सोर्स बनाने के लिए धन्यवाद! –

संबंधित मुद्दे