2012-03-23 8 views
19

アプリAstridタスクにはボタンがあります。ボタンを押すと、ドロップダウンメニューが表示されます。 カスタムスピナー/ドロップダウンメニュー

enter image description here

enter image description here

それは基本的にスピナーだが、ドロップダウン・リスト形式で

似たようなことを誰かが知っていますか?これは私が見ていないウィジェットですか?

+0

コンテキストメニュー –

答えて

55

これの元の著者(私はアストリッドのAndroid開発者の一人です)として、アストリッドがどのようにしているのかをお伝えしたいと思います。私はここに基礎を掲示しますが、詳細はgithub repo(https://github.com/todoroo/astrid)で見つけることができます。基本的な考え方は、GreenDroidのQuickActionWidgetをハンナリーが示唆するように拡張することです。

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

これは素晴らしいです!あなたは素晴らしいです、ありがとう! (P.S. Love Astrid!) – Cole

+14

オープンソース化してくれてありがとう! –