2012-05-24 90 views
0

我正在開發一個需要繪製用戶簽名的應用程序。點擊一個按鈕,我需要顯示上下文菜單,用戶可以在其中選擇要繪製的線條粗細。任何人都可以幫助我創建具有不同線粗度的上下文菜單。即我想要一條最高厚度的線條作爲上下文菜單的第一項目,並且最低厚度的線條作爲上下文菜單的最後一項目。請幫幫我。行在android中的上下文菜單?

+0

創建自定義佈局並在上下文菜單中設置佈局 –

回答

0

您可以顯示AlertDialog而不是上下文菜單,以選擇按鈕單擊時的線條粗細。

例如,

public void lineButtonClick(View v) { 
        Builder builder = new Builder(this); 
        builder.setTitle("Line Thickness"); 
        builder.setAdapter(new LineAdapter(this), new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // here you get index of clicked item in "which" parameter 
         } 
        }); 
        builder.create().show(); 
       } 

       class LineAdapter extends BaseAdapter { 
        // these are the line images with different thickness 
        int lineIds[] = { 
         R.drawable.line1, R.drawable.line2, R.drawable.line3, R.drawable.line4, R.drawable.line5  
        }; 

        @Override 
        public int getCount() { 
         return lineIds.length; 
        } 

        @Override 
        public Object getItem(int arg0) { 
         return arg0; 
        } 

        @Override 
        public long getItemId(int position) { 
         return position; 
        } 

        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
         ViewHolder holder; 
         if(convertView == null) { 
          convertView = getLayoutInflater().inflate(R.layout.raw_line, null); 
          holder = new ViewHolder(); 
          holder.imgLine = (ImageView)convertView.findViewById(R.id.imgLine); 
          convertView.setTag(holder); 
         } else { 
          holder = (ViewHolder) convertView.getTag(); 
         } 

         holder.imgLine.setImageResource(lineIds[position]); 
         return convertView; 
        } 

        class ViewHolder { 
         ImageView imgLine; 
        } 
       } 

    // raw_line.xml 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/imgLine" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

    </LinearLayout>